2017-10-27 3 views
0

私はsample_layout.xmlという名前のレイアウトを持っていますが、は複数回使用しますが、のたびにを使用します。DataBindingを除いて、Android Studioで複数の異なる属性を持つ同じレイアウトを複数回使用する方法はありますか?

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="vertical" 
    android:layout_marginBottom="20dp"> 

    <RelativeLayout 
    android:layout_width="match_parent" 
    android:layout_height="110dp" 
    android:background="@drawable/full_panel_blue" 
    android:padding="10dp" 
    android:layout_marginBottom="20dp"> 

    <ImageView 
     android:layout_width="120dp" 
     android:layout_height="120dp" 
     android:src="@mipmap/first_icon" /> 

    <LinearLayout 
     android:layout_width="350dp" 
     android:layout_height="fill_parent" 
     android:layout_alignParentRight="true" 
     android:gravity="end" 
     android:orientation="vertical"> 

     <TextView 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:textColor="@color/my_white" 
      android:text="4000000" 
      android:textSize="50sp" 
      android:gravity="end"/> 

     <TextView 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:gravity="end" 
      android:textColor="@color/my_white" 
      android:textSize="20sp" 
      android:text="Sample text" /> 

    </LinearLayout> 

    </RelativeLayout> 

</LinearLayout> 

低いが、それはそう何回

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="vertical"> 

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

</LinearLayout> 

にも含まれますレイアウトですが、私はそれにそれがあると同じように最初の時間を含めたいが、2番目の の代わりに@drawable/full_blue_panelを使用しました。別の ドロアブル(@drawable/red_w_transparency)を使用します。また、 ImageViewについてもう一度別のアイコンをsrcに、もう一度アイコンを もう一度使いたいです。以前に作成された属性のいずれかの場合は、毎回TextView の異なるテキストなどがあります。私は 既にDataBindingで何かを試しましたが、私はそれが にちょうど適しているとは思いません。これに他の方法はありますか?

答えて

1

一般的に、カスタムViewサブクラスを作成することでこの種の問題を解決します。 https://developer.android.com/guide/topics/ui/custom-components.html#compound

短いバージョンでは、変更する部分に影響を与えるカスタム属性を定義し、その属性を読み取ってビュー階層を変更するViewサブクラス実装を作成します。あなたのポストされた例では

、あなたはこれらの関連ポイントがあります。

  • を根には、あなたがImageView SRC
  • にあなたを変更したいRelativeLayout背景
  • を変更したいLinearLayout
  • ですTextViewテキストを変更したい

あなたのattrs.xmlでこれらの属性を作成することができます。

<declare-styleable name="MyCompoundComponent"> 
    <attr name="background"/> 
    <attr name="imageSrc"/> 
    <attr name="primaryText"/> 
</declare-styleable> 

次に、あなたはあなたのビュークラスを作成します。

public class MyCompoundComponent extends LinearLayout { 

    public MyCompoundComponent(Context context, AttributeSet attrs) { 
     super(context, attrs); 

     TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.MyCompoundComponent); 
     // read your attributes 
     a.recycle(); 

     inflate(context, R.layout.my_compound_component, this); 

     // find your views, use your attributes to modify them 
    } 
} 

そして、もちろん、あなたがレイアウトを必要とする:あなたの元のレイアウトのルートはLinearLayoutだったので、私たちはそこから派生します。あなたはLinearLayoutの中でそれを膨らませているので、ルート要素を<merge>に変更することを除いて、上に投稿したものを使うことができます。[OK]を、私はすべてのことを行っているが、それでも私はそれがどの `知っているだろうか見ていけない

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="vertical"> 

    <com.example.stackoverflow.MyCompoundComponent 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     app:background="@color/blue" 
     app:imageSrc="@drawable/image1" 
     app:primaryText="@string/text1"/> 

    <com.example.stackoverflow.MyCompoundComponent 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     app:background="@color/red" 
     app:imageSrc="@drawable/image2" 
     app:primaryText="@string/text2"/> 

</LinearLayout> 
+0

の場所にあるすべてのものと

、あなただけの他のビューのようなあなたの他のレイアウトでは、このビューを使用することができますImageSrc'は2つの 'ImageView'があるのでカスタムレイアウトで変更されます。または、カスタムビューにもっと多くのテキストがあるので変更する必要のあるテキストを知る方法は(入れ子になっています) –

+0

複数の属性を作成します。おそらく ''と ''または '' vs ' 'など。コード内のそれらを取得し、それらを適切なビューに割り当てることができます。 –

関連する問題