/ / Androidカスタムビューを展開できません-android、android-inflate

アンドロイド、android-inflate

アクションバーのカスタムビューを作成します。 <inclued layout=@layout/XXXXX />しかし、私がLayoutInflaterを使用してビューを膨張させると、それを膨張させることはできません。

public class TitleLayout extends LinearLayout {
//    private Button btn_back;
//    private Button btn_edit;
public TitleLayout(Context context, AttributeSet attrs) {
super(context, attrs);
LayoutInflater.from(context).inflate(R.layout.view_title,this);
}
}

TitleLayoutは私のカスタムビューです

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btn_back"
android:layout_gravity="center"
android:text="Back"/>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:id="@+id/text_title"
android:text="This is a title"
android:layout_gravity="center"
android:textSize="30sp"
android:gravity="center"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:id="@+id/btn_edit"
android:text="Edit"/>

</LinearLayout>

これは単純なビューです。使用するとき <inclued /> 膨らませることは大丈夫です。ただし、LayoutInflateを使用しても何も表示されません。

<!--<include-->
<!--layout="@layout/view_title"/>-->
<com.example.administrator.test.TitleLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
</com.example.administrator.test.TitleLayout>

あなたの助けに本当に感謝します!!!

回答:

回答№1では-2

これを試して

public class TitleLayout extends LinearLayout  {

public TitleLayout (Context context) {
super(context);

LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
layoutInflater.inflate(R.layout.your_layout, this);
}
}