具有自定义XML布局的按钮

f0r*_*0rz 8 xml android button

是否可以创建具有自定义xml布局的按钮?

我有这个布局:

<?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>
Run Code Online (Sandbox Code Playgroud)

现在我想在一个按钮上使用它.谁知道我怎么做到这一点?
我在想是否有Button.java文件扩展了Button.然后是setView(R.layout.mylayout.xml); ......但这很容易,而且显然不起作用

关心马丁

din*_*igo 23

我最近一直在处理这个问题,因为我想在一个按钮中放置两个textview.你必须选择:

  1. 扩展Button类并在布局中使用它
  2. 使用一个按钮的布局并在你需要的内部拖动,并通过向其添加此参数来使其可点击:

    android:clickable="true"
    
    Run Code Online (Sandbox Code Playgroud)

之后,您可以通过定义来修改布局的外观

android:background="@drawable/my_background"
Run Code Online (Sandbox Code Playgroud)

给它一个"按钮式"的面孔和行为

/res/drawable/mi_background.xml

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android">   
    <item android:state_focused="true" android:drawable="@color/black"/> 
    <item android:state_pressed="true" android:state_enabled="false" android:drawable="@color/black" />
    <item android:drawable="@color/white"/> 
 </selector>
Run Code Online (Sandbox Code Playgroud)


Com*_*are 7

你无法在面对面使用那种布局Button.但是,您可以使用a上的android:drawableRight属性实现类似的外观Button.