按键采用自定义XML布局自定义、按键、布局、XML

2023-09-07 16:48:33 作者:新生

是否有可能创建一个自定义XML布局的按钮?

Is it possible to create a button with a custom xml layout?

我有这样的布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:padding="1dp"
  android:background="#7e7e7e">

 <RelativeLayout
   xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:padding="10dp"
   android:background="#f9f9f9">

 <TextView
  android:id="@+id/TextView01" 
  android:layout_width="wrap_content" 
  android:layout_height="wrap_content"
  android:layout_alignParentLeft="true"
  android:text="ButtText"
  android:textColor="#000000">
 </TextView>

 <ImageView 
  android:id="@+id/ImageView01" 
  android:layout_width="wrap_content" 
  android:layout_height="wrap_content" 
  android:background="@drawable/arrow"
  android:layout_alignParentRight="true">
 </ImageView>

 </RelativeLayout>
</LinearLayout>

现在我想用这个按钮上。任何人都知道我该怎么做呢?结果我在想,如果我有这样的扩展按钮Button.java文件。然后的setView(R.layout.mylayout.xml); ...但是,这是为了方便,它显然不工作

Now I want to use this on a button. Anyone know how I can do this? I was thinking if I had Button.java file that extended Button. And then setView(R.layout.mylayout.xml); ... but that was to easy, and it clearly not working

问候马丁

推荐答案

您不能随便使用该布局在按钮面对。您可以使用机器人实现类似的外观:drawableRight 按钮属性,虽然

You cannot literally use that layout on the face of a Button. You can achieve a similar look using the android:drawableRight property on a Button, though.