2017-06-03 13 views
1

から一定の割合であることを行い、その後、私はどのようにこのは、私は、画像を水平にし、画面の上部の中心になりたい場合は画像サイズが制約レイアウトでは、画面サイズ

<android.support.constraint.ConstraintLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_marginTop="10dp"> 
     <ImageView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:src="@drawable/image" 
      app:layout_constraintLeft_toLeftOf="parent" 
      app:layout_constraintRight_toRightOf="parent" 
      android:id="@+id/imageView" /> 
</android.support.constraint.ConstraintLayout> 

を行うことができます画像の幅と高さを画面の幅と高さの25%にすることはできますか?これは実行可能ですか?

は、あなたがこのようにプログラム的にそれを設定することができ、あなたに

答えて

0

更新:それはこれを行うには非常に良くな方法を実施しています新しいConstrainLayout 1.1.0

<?xml version="1.0" encoding="utf-8"?> 
<android.support.constraint.ConstraintLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    xmlns:app="http://schemas.android.com/apk/res-auto"> 

    <ImageView 
     android:layout_width="0dp" 
     android:layout_height="0dp" 
     android:background="#ffff0000" 

     app:layout_constraintLeft_toLeftOf="parent" 
     app:layout_constraintRight_toRightOf="parent" 
     app:layout_constraintTop_toTopOf="parent" 
     app:layout_constraintBottom_toBottomOf="parent" 

     app:layout_constraintWidth_percent=".25" 
     app:layout_constraintWidth_default="percent" 

     app:layout_constraintHeight_percent=".25" 
     app:layout_constraintHeight_default="percent" 
     /> 

</android.support.constraint.ConstraintLayout> 

それとも

あなたは割合にガイドラインを配置し、それらにImageViewのを制約することができます。 Androidは2がされたオーバーライドメソッド(onMeasure)を提供する、それを行うためのクリーンな方法があると便利だろうが、私はそれがすべてのビューでは重み

<?xml version="1.0" encoding="utf-8"?> 
<android.support.constraint.ConstraintLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    xmlns:app="http://schemas.android.com/apk/res-auto"> 

    <ImageView 
     android:id="@+id/imageView" 
     app:layout_constraintLeft_toLeftOf="@id/guideline_left" 
     app:layout_constraintRight_toRightOf="@id/guideline_right" 
     app:layout_constraintTop_toTopOf="@id/guideline_top" 
     app:layout_constraintBottom_toBottomOf="@id/guideline_bottom" 
     android:layout_width="0dp" 
     android:layout_height="0dp" 
     android:background="#ffff0000"/> 

    <android.support.constraint.Guideline 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/guideline_left" 
     android:orientation="vertical" 
     app:layout_constraintGuide_percent="0.33"/> 

    <android.support.constraint.Guideline 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/guideline_right" 
     android:orientation="vertical" 
     app:layout_constraintGuide_percent="0.66"/> 


    <android.support.constraint.Guideline 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/guideline_top" 
     android:orientation="horizontal" 
     app:layout_constraintGuide_percent="0.33"/> 

    <android.support.constraint.Guideline 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/guideline_bottom" 
     android:orientation="horizontal" 
     app:layout_constraintGuide_percent="0.66"/> 

</android.support.constraint.ConstraintLayout> 
+0

ああ洙クールthatsの。最後に彼らはパーセンテージを持っていますが、これはサポートライブラリにありますか? – Snake

+0

これは本当に涼しいです、私は最終的にLinearLayoutにさよならを言うつもりだと思います。 1.1.0のバージョンで、ベータ版になっています。あなたは[こちらを読む](https://androidstudio.googleblog.com/2017/05/constraintlayout-110-beta-1-release)でも簡単に使えます。 html) – lelloman

+0

ここでも同じです。パーセンテージがあれば他のレイアウトは必要ないと思うので、複数のスクリーンをより簡単にサポートできます。私はリンクをクリックして、2行の横の使用法に関する情報はありません。ベータ版では、実行中のデバイス上でアプリで使用することはできますか? – Snake

0

をありがとう: -

ImageView image_view = (ImageView) findViewById(R.id.image_view); 
calulate(image_view); 



private void calulate(View v) 
{ 
    int imgWidth = this.getResources().getDisplayMetrics().widthPixels; 
    int imgHeight = this.getResources().getDisplayMetrics().heightPixels; 
    v.getLayoutParams().height = (int) (0.25*imgHeight); 
    v.getLayoutParams().width = (int) (0.25*imgWidth); 
} 
0

でまだネストされたのLinearLayoutよりもはるかに良いことだと思いますパラメータ(int widthMeasureSpec, int heightMeasureSpec)、要件に応じて高さ幅を返すことができます。 例:あなたがしたい場合は、高さ=幅は、あなたがこのように高さと幅を返すことができ、正方形サイズのビューを意味します

@Overrideprotected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) 
{ 
    super.onMeasure(heightMeasureSpec, heightMeasureSpec); 
} 

あなたはこのためにカスタムビューを作成することができます 例: CustomImageViewはして拡張するクラス名を作りますこのようなレイアウトでImageViewの

public class CustomImageView extends ImageView 
{ 

    @Override  
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) 
    { 
     super.onMeasure(heightMeasureSpec, heightMeasureSpec); 

     // you can set this as per percentage also. For example: 
     //"heightMeasureSpec * 50 /100" ---- the height will be 50% of the 
     //view's width. 
    } 

} 

使用これを:

<CustomImageView  
    android:id="@+id/imageView"  
    android:layout_width="match_parent"  
    android:layout_height="wrap_content" 
    android:src="@drawable/ic_launcher"  
/> 
関連する問題