Я меняю некоторые цвета фона кнопок перед отображением активности, и когда кнопки отображаются, вокруг них отсутствует белое пространство.
Как сделать кнопки красными там, где серые, и сохранить белые интервалы?
2 ответа
Вы, вероятно, пропустили настройку поля для макета. На самом деле вокруг вашей кнопки есть белое пространство, поэтому все кнопки касаются, поэтому, когда вы устанавливаете фон, это белое пространство также становится красным. Это должно быть хорошо, я думаю.!
Макет:
<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);
Выход
Создайте red_button.xml
в drawable
следующим образом:
<?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);
Похожие вопросы
Новые вопросы
android
Android — это мобильная операционная система Google, используемая для программирования или разработки цифровых устройств (смартфонов, планшетов, автомобилей, телевизоров, одежды, очков, IoT). Для тем, связанных с Android, используйте теги, специфичные для Android, такие как android-intent, android-activity, android-adapter и т. д. Для вопросов, отличных от разработки или программирования, но связанных с Android framework, используйте эту ссылку: https://android .stackexchange.com.