[英]Create custom layout in XML to initialize in code or in another layout xml
我在Android中創建自定義布局時遇到了試圖解決(或更好地理解應做的方式)的問題 。
我想創建一個自定義的RelativeLayout
類供我使用,該類在布局XML文件中定義。
my_relative_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<com.mypackage.MyRelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/image_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@null"
android:scaleType="fitStart"
android:src="@drawable/my_drawable"/>
<TextView
android:id="@+id/text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/title_gray"
android:layout_below="@id/image_view"
android:gravity="center"
android:text="@string/placeholder" />
</com.mypackage.MyRelativeLayout>
用法
public class MyRelativeLayout extends RelativeLayout {
private AttributeSet attrs;
private ImageView imageView;
private TextView textView;
public MyRelativeLayout(Context context) {
super(context);
}
public MyRelativeLayout(Context context, AttributeSet attrs) {
super(context, attrs);
this.attrs = attrs;
}
public MyRelativeLayout(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
this.attrs = attrs;
}
@Override
protected void onFinishInflate() {
super.onFinishInflate();
if (attrs != null) {
TypedArray a = getContext().getTheme().obtainStyledAttributes(attrs, R.styleable.MyRelativeLayout, 0, 0);
drawableResource = a.getResourceId(R.styleable.MyRelativeLayout.image_view, 0);
a.recycle();
}
imageView = (ImageView) findViewById(R.id.image_view);
textView = (TextView) findViewById(R.id.text_view);
if (drawableResource != 0 && imageView != null) {
imageView.setImageResource(drawableResource);
}
}
}
我的問題是我想在另一個XML和代碼中初始化此布局。 但是當我編寫類和XML時,我只能通過執行以下操作在代碼中使用它:
myLayout = (MyRelativeLayout) LayoutInflater.from(this.getActivity()).inflate(R.layout.my_relative_layout, container, false);
用其他XML編寫以下內容時,會導致onFinishInflate在getViewById上失敗(返回null),並且childrenCount為0
<com.mypackage.MyRelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="@id/another_layout"
app:image_view="@drawable/my_image" />
並執行以下操作,就不會讓我配置自定義image_view屬性。
<include layout="@layout/my_relative_layout"/>
為了解決這個問題,我可以將自定義布局XML根元素更改為RelativeLayout類型,並將以下內容添加到onFinishInflate方法的開頭:
LayoutInflater.from(getContext()).inflate(R.layout.my_relative_layout, this, true);
但是XML不會引用我的課程。
我的問題是
1.我在自定義布局的定義中缺少什么嗎?
2.自定義布局的正確定義是什么?
先感謝您!
首先,您應該在承包商中使用樣式化的屬性,如本示例所示。
您如何對MyRelativeLayout定義為休假的期望:
<com.mypackage.MyRelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="@id/another_layout"
app:image_view="@drawable/my_image" />
要知道my_relative_layout.xml
中定義的my_relative_layout.xml
嗎?
要使其工作,您應該創建一個服裝布局並將其手動添加到com.mypackage.MyRelativeLayout
。
像這樣的東西:
costume_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/image_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@null"
android:scaleType="fitStart"
android:src="@drawable/my_drawable"/>
<TextView
android:id="@+id/text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/title_gray"
android:layout_below="@id/image_view"
android:gravity="center"
android:text="@string/placeholder" />
</RelativeLayout>
您的服裝視角
public class MyRelativeLayout extends RelativeLayout {
private AttributeSet attrs;
private ImageView imageView;
private TextView textView;
public MyRelativeLayout(Context context) {
super(context);
init();
}
public MyRelativeLayout(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public MyRelativeLayout(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
private void init(Context context){
LayoutInflater.from(context).inflate(R.layout.costume_layout, this);
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.