Android 我无法扩充自定义视图
Android I cannot inflate custom view
我为 actionbar.When 创建了一个自定义视图,我使用 <inclued layout=@layout/XXXXX />
是 ok.However ,当我使用 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>
非常感谢您的帮助!!!
试试这个
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);
}
}
我为 actionbar.When 创建了一个自定义视图,我使用 <inclued layout=@layout/XXXXX />
是 ok.However ,当我使用 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>
非常感谢您的帮助!!!
试试这个
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);
}
}