0

私はそれLinearLayoutにいくつかを持ってViewsいます。次に、この要素を として扱いたいViewので、 から新しいclass拡張を作成しましたLinearLayoutinstanceこれclassをレイアウトに動的に追加すると、何も表示されません。Viewからどうにかして を取得する必要があると思いますが、class方法がわかりません。この新しいクラスを何らかの方法で xml に関連付けることは可能ですか?

アップデート:

public class Task extends LinearLayout {

    public Task(Context context) {
        super(context);
        LayoutInflater.from(context).inflate(R.layout.task_view, this, false);
    }

    public Task(Context context, AttributeSet attrs) {
        super(context, attrs);
        LayoutInflater.from(context).inflate(R.layout.task_view, this, false);

    }

    public Task(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        LayoutInflater.from(context).inflate(R.layout.task_view, this, false);
    }
}

それで:

Task newTask = new Task(getActivity());
someLinearLayout.addView((View) newTask); // happens nothing
4

1 に答える 1

0

次のようなさまざまなアプローチを使用できます。

膨らませる:

public class InflatedView extends LinearLayout
{
      public InflatedView(Context c)
      {
            super(c);
            LayoutInflater.from(this).inflate(R.layout.your_other_layout, this);
      }
      //override other constructors too.
}

これで、次のように xml でこれを使用できます。

<com.your.package.InflatedView android:layout_height="etc" other:attribute="here" />

含む:

非常にシンプルで、include タグを使用します。

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

RomainGuy のレイアウト トリックは次のとおりです。

于 2012-12-22T13:54:45.177 に答える