2017-01-22 4 views
0

同じコードを繰り返すのは嫌です。Androidにはパラメータ化可能なレイアウトが含まれています

<include layout="@layout/tmpl_stars22" /> 

とを有する第二:

<include layout="@layout/tmpl_stars36" /> 

含まれるレイアウトが含まれています

.... 
<ImageView 
    android:id="@+id/star1" 
    android:layout_width="22dp" 
    android:layout_height="22dp" 
    android:src="@drawable/ic_star_off" 
    tools:ignore="ContentDescription" /> 
.... 

第2のレイアウトは、36dpの画像を使用して私は1つので活性を有しています。どうにかして2つのファイルがあるのを避けることはできますか私は含まれているレイアウトの中にいくつかのパラメータを渡す方法を見つけていません。 https://developer.android.com/training/improving-layouts/reusing-layouts.html

結論 -

+0

私はこのレイアウトをカスタムビューに変換するべきだと思います。 – K2evil

答えて

1

カスタムビューを作成してレイアウトで使用できます。

exmaple

custom_layout.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"> 

    <ImageView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/imgview" /> 
</LinearLayout> 

CustomView.java

public class CustomView extends LinearLayout { 

    int width = -1; 
    int height = -1; 
    ImageView imageview; 

    public CustomView(Context context) { 
     super(context); 
     inflate(context, R.layout.custom_layout, this) 
     initViews(); 
    } 

    @SuppressWarnings("ResourceType") 
    public CustomView(Context context, AttributeSet attrs) { 
     super(context, attrs); 
     inflate(context, R.layout.custom_layout, this); 
     int[] attributes = { 
       android.R.attr.width,  //=====> 0 
       android.R.attr.height  //=====> 1 
     }; 
     TypedArray attrSet = context.obtainStyledAttributes(attrs, attributes); 
     width = attrSet.getDimension(0, -1); //android.R.attr.width, 
     height = attrSet.getDimension(1, -1); //android.R.attr.height 
     attrSet.recycle(); 
     initViews(); 
    } 

    public CustomView(Context context, AttributeSet attrs, int defStyleAttr) { 
     super(context, attrs, defStyleAttr); 
     inflate(context, R.layout.custom_layout, this); 
     initViews(); 
    } 

    public void initViews(){ 
     imageview = findViewById(R.id.imgview); 
     if(width > -1){ 
      imageview.setWidth(width); 
     } 
     if(height > -1){ 
      imageview.setHeight(height); 
     } 
    } 
} 

、あなたはそれを使用したい場合:

activity.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <CustomView 
     android:layout_width="33dp" 
     android:layout_height="33dp" /> 
</LinearLayout> 

基本構造を表示しようとしましたが、このコードをカスタマイズする必要があります。

+0

ありがとう、しかしこの場合、カスタムビューは過剰です。 –

+0

@LeosLiterakその場合、あなたの質問の答えはまっすぐな "いいえ"です...コードを使わずに動的なデータをXMLファイルに設定する方法はありません。 – K2evil

+0

わかりました。 –

2

タグが含まにあなたはサイズを追加することができますことはできません:(:layout_ *属性の任意アンドロイド)の

<include layout="@layout/tmpl_stars" 
    android:layout_width="22dp" 
    android:layout_height="22dp" /> 

<ImageView 
    android:id="@+id/star1" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:src="@drawable/ic_star_off" 
    tools:ignore="ContentDescription" /> 

あなたはまた、すべてのレイアウトパラメータを上書きすることができますがタグに指定することで、レイアウトのルートビューを含めました。例:

+0

素敵なハックですが、画像は含まれているレイアウトの一部に過ぎません。とにかくレイアウト情報を渡すために上書きされます。 –

+0

だから私は解決策がありません – Yoleth

+0

または子供に適用する親からのいくつかのパラメータでカスタムビューを作成することができます – Yoleth

関連する問題