我已经用XML定义了一个布局,现在我想用它来制作一个自定义按钮(它只需要显示布局并可以单击即可,我不在乎不同的状态和东西)。我不太确定下一步该怎么做。如何在我的主要活动中创建它?
谢谢!
我的自定义XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="25dp"
android:layout_marginTop="10dp"
android:textSize="20dp"
android:text="New Text"
android:id="@+id/text" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_marginRight="25dp"
android:layout_alignParentRight="true"
android:src="@drawable/left"
android:adjustViewBounds="true"
android:scaleType="centerCrop"
android:maxHeight="30dp"
android:maxWidth="30dp"/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/text"
android:height="1dp"
android:background="@color/dim_foreground_material_dark"
android:layout_marginLeft="25dp"
android:layout_marginTop="10dp"
android:text=""
android:id="@+id/horiz" />
</RelativeLayout>
让我们从探索如何创建自定义视图开始:
1)子类化内置布局之一。
2)在构造函数中添加合并布局。
3)使用findViewById()初始化成员以指向内部视图。
4)添加您自己的API来查询和更新视图状态。
所以我认为您想使用第二个选项:
public class ButtonView extends Button {
public ButtonView (Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public ButtonView (Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
LayoutInflater.from(context).inflate(R.layout.custom_xml, this, true);
}
}
要进一步阅读,请查看:
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句