带自定义XML布局的按钮
问题描述:
是否可以使用自定义xml布局创建按钮?带自定义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); ...但是,这是为了方便,而这显然不工作
问候马丁
答
你不能随便使用布局上Button
的脸。不过,您可以使用Button
上的android:drawableRight
属性来实现类似的外观。
答
最近我一直在处理这个问题,因为我想在按钮中放两个文本视图。你必须选择:
- 扩展Button类,并在布局
-
使用布局使用它insted的一个按钮,并拖动everithing需要,并使其可点击加入这个parametter它:
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>
一切都很简单。谢谢你今天的信息! :) – f0rz 2010-02-25 17:26:13