![](/img/trans.png)
[英]When setting background color to button in android it Hides White Space between buttons in linear layout?
[英]Changing button background color removes white space
我在顯示活動之前和顯示按鈕時更改了一些按鈕背景色,但它們缺少周圍的空白。
如何使按鈕變成紅色(灰色在哪里)並保持白色間距?
像這樣在drawable
創建red_button.xml
:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<stroke android:width="2dp"
android:color="#80FFFFFF" />
<corners android:radius="25dp" />
<gradient android:angle="270"
android:centerColor="#ff0000"
android:endColor="#ff0000"
android:startColor="#ff0000" />
</shape>
獲得與默認Button
相同的形狀
您可以隨心所欲地使用radius
, stroke width
和stroke color
來獲得Button
。
編輯:您可以將顏色添加到原始默認Button
如下所示:
Drawable d = yourButton.getBackground();
PorterDuffColorFilter filter = new PorterDuffColorFilter(Color.RED, PorterDuff.Mode.SRC_ATOP); // or whatever color you like
d.setColorFilter(filter);
您可能錯過了為版面設置邊距的設置。 實際上,您的按鈕周圍有一個空格,因此所有按鈕都被觸摸了,因此當您設置背景時,空格也會變成紅色。 我猜應該沒關系!
版面:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<Button
android:id="@+id/one"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="About"
android:layout_margin="5dp" />
<Button
android:id="@+id/two"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Gallery"
android:layout_margin="5dp"/>
<Button
android:id="@+id/third"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Media"
android:layout_margin="5dp"/>
</LinearLayout>
在strings.xml中
<color name="red">#FF0000</color>
在Java中
one = (Button) findViewById(R.id.one);
two = (Button) findViewById(R.id.two);
three = (Button) findViewById(R.id.third);
one.setBackgroundResource(R.color.red);
two.setBackgroundResource(R.color.red);
three.setBackgroundResource(R.color.red);
輸出量
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.