【问题标题】:Button with custom XML layout具有自定义 XML 布局的按钮
【发布时间】:2011-01-20 23:56:27
【问题描述】:

是否可以创建具有自定义 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 文件。然后 setView(R.layout.mylayout.xml); ...但这很容易,而且显然不起作用

问候马丁

【问题讨论】:

    标签: android xml button


    【解决方案1】:

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

    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>
    

    【讨论】:

      【解决方案2】:

      您不能在Button 的表面上使用该布局。不过,您可以使用 Button 上的 android:drawableRight 属性获得类似的外观。

      【讨论】:

      • 一切都那么简单。谢谢你今天的信息! :)
      【解决方案3】:

      您可以使用 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>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-10-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-03-09
        • 1970-01-01
        相关资源
        最近更新 更多