Java 如何在android中设置按钮中心的图像和文本
我已经创建了六个按钮,带有图像和文本。。。。。。我只想把这些图片和文字放在按钮的中心注意,文字就在图片下面,这个。。。是我的XML代码。。。如果有人能解决这个问题,就在楼下写XML代码,对不起,我的英语不好,我希望这是可以理解的Java 如何在android中设置按钮中心的图像和文本,java,android,Java,Android,我已经创建了六个按钮,带有图像和文本。。。。。。我只想把这些图片和文字放在按钮的中心注意,文字就在图片下面,这个。。。是我的XML代码。。。如果有人能解决这个问题,就在楼下写XML代码,对不起,我的英语不好,我希望这是可以理解的 <?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:lay
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="3" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="horizontal"
android:weightSum="2" >
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:drawableTop="@drawable/list"
android:text="Order Station"
android:textStyle="bold"/>
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="2" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="horizontal"
android:weightSum="2" >
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:drawableTop="@drawable/power"
android:background="#ff4545"
android:text="Quick Order"
android:textStyle="bold" />
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="4"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="horizontal"
android:weightSum="2" >
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:drawableTop="@drawable/coffeecup"
android:text="Take Away"
android:textStyle="bold"/>
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="4" />
</LinearLayout>
</LinearLayout>
</FrameLayout>
或者您必须使用另一种布局(线性或相对),将drawableTop放置在文本视图上,并通过提供onClickListener来使用该线性布局作为按钮
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="3" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="horizontal"
android:weightSum="2" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:layout_weight="1"> //give onclick to this layout to act as a button
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:drawableTop="@drawable/list"
android:gravity="center"
android:text="Order Station"
android:textStyle="bold"/>
</LinearLayout>
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="2" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="horizontal"
android:weightSum="2" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:background="#ff4545"
android:layout_weight="1">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:drawableTop="@drawable/power"
android:gravity="center"
android:text="Quick Order"
android:textStyle="bold"/>
</LinearLayout>
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="4"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="horizontal"
android:weightSum="2" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:layout_weight="1">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:drawableTop="@drawable/coffeecup"
android:gravity="center"
android:text="Take Away"
android:textStyle="bold"/>
</LinearLayout>
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="4" />
</LinearLayout>
</LinearLayout>
//单击此布局作为按钮
或者将按钮的高度设置为包裹内容
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="horizontal"
android:weightSum="2" >
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:drawableTop="@drawable/list"
android:text="Order Station"
android:textStyle="bold"/>
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="2" />
</LinearLayout>
如果您希望裁剪图像并保持文本居中,只需将图像设置为按钮的背景:
android:background=“@drawable/YOUR_IMAGE”
以下是它作为xml的外观:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="3" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="horizontal"
android:weightSum="2" >
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="Order Station"
android:textStyle="bold"
android:background="@drawable/list" />
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="2" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="horizontal"
android:weightSum="2" >
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="Quick Order"
android:textStyle="bold"
android:background="@drawable/power" />
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="4"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="horizontal"
android:weightSum="2" >
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="Take Away"
android:textStyle="bold"
android:background="@drawable/coffeecup"/>
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="4" />
</LinearLayout>
</LinearLayout>
</FrameLayout>
您想裁剪图像以适应按钮大小作为背景,而文本仍然居中吗?非常感谢,但这确实很有帮助,但我将此按钮更改为相对布局,因为我可以根据需要放置图像和文本视图。是的,实际上我想添加另一个布局-线性或相对。非常感谢,但这确实很有帮助,但我改变了此按钮用于相对布局,因为我可以根据需要放置图像和文本视图–7arooney