使用自定义XML布局的按钮

9

是否可以创建一个具有自定义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>

现在我想在一个按钮上使用它。 有人知道我该怎么做吗? 我在想如果我有一个扩展Button的Button.java文件。 然后设置View(R.layout.mylayout.xml); ... 但那太简单了,显然不起作用。
敬礼 Martin
3个回答

25

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

  1. 扩展Button类并在布局中使用它
  2. 使用布局代替按钮,将需要的所有内容拖放到其中,并通过添加此参数使其可点击:

    android:clickable="true"
    
    之后,您可以通过定义来修改布局的外观。
    android:background="@drawable/my_background"
    

    将其呈现为具有“按钮式”外观和行为

    /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>
    

9

你不能直接在一个 Button 上使用那个布局。不过,你可以通过在 Button 上使用 android:drawableRight 属性来实现类似的外观。


一切都很容易。感谢您今天的信息! :) - f0rz

0
你可以使用RelativeLayout将自定义按钮视图简单地覆盖在真实的Button上,像这样:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <Button
        android:id="@+id/button1"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <!-- Your custom button layout -->
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#7e7e7e"
        android:padding="1dp">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="#f9f9f9"
            android:padding="10dp">

            <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" />

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

        </RelativeLayout>
    </LinearLayout>
</RelativeLayout>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接