Android 具有自定义XML布局的按钮
是否可以创建具有自定义xml布局的按钮 我有这样的布局:Android 具有自定义XML布局的按钮,android,xml,button,Android,Xml,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
<?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)。。。但这很容易,而且显然不起作用
马丁你不能在
按钮
上使用那种布局。不过,使用按钮上的android:drawableRight
属性可以获得类似的外观。我最近一直在处理这个问题,因为我想在一个按钮中放置两个文本视图。你必须选择:
扩展Button类并在布局中使用它
使用布局而不是按钮,在需要的地方拖动,并通过添加以下参数使其可单击:
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>
您可以使用RelativeLayout将自定义按钮视图简单地覆盖在真实按钮上,如下所示:
<?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>
一切都很简单。感谢您提供今天的信息!:)