【发布时间】:2018-03-16 18:13:01
【问题描述】:
我正在尝试将两个图像按钮和一些文本放在一行上。这是布局的 XML:
<?xml version="1.0" encoding="utf-8"?>
<mycompany xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="28dp"
android:layout_marginTop="5dp"
android:orientation="horizontal">
<ImageButton
android:id="@+id/sectionDelete"
android:layout_width="35dp"
android:layout_height="28dp"
android:layout_alignParentLeft="true"
android:src="@drawable/button_delete" />
<TextView
android:id="@+id/header"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/sectionDelete"
android:text="test"
android:textAllCaps="true"
android:textColor="@color/navigation_bar"
android:textSize="16sp"
android:textStyle="bold" />
<ImageButton
android:id="@+id/sectionAdd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:background="@drawable/button_add" />
</RelativeLayout>
<View
android:id="@+id/line"
android:layout_marginTop="8dp"
android:layout_width="match_parent"
android:layout_height="2dp"
android:background="@android:color/darker_gray" />
</LinearLayout>
</mycompany>
drawable 中每个按钮的选择器 XML:
button_delete.xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:drawable="@drawable/delete_button_pressed"
android:state_pressed="true" />
<item android:drawable="@drawable/delete_button"
android:state_focused="true" />
<item android:drawable="@drawable/delete_button" />
</selector>
button_add.xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:drawable="@drawable/button_add_pressed"
android:state_pressed="true" />
<item android:drawable="@drawable/button_add_normal"
android:state_focused="true" />
<item android:drawable="@drawable/button_add_normal" />
</selector>
在构建器中一切看起来都很好:
但在应用程序中,灰色背景会丢失,图像的边缘(透明)会显示出来,但仅适用于第一张图像:
奇怪的是,第一个图像按钮无法识别图像的透明背景。此外,我需要弄乱RelativeLayout 和第一个ImageButton 的宽度和高度,以使其接近正确的大小。第二次我不需要做任何事情。第一张图没什么特别的。
以下是目录中的图片:
最后一个问题 - 如果空间太长,如何让第二张图片之前的文本换行?现在它在包装之前写在第二张图片下:
这里是所有删除的图像。似乎有透明的背景,但我远非 Gimp 专家。也不确定 StackOverflow 是否保留原版..
更新
我已验证图像是透明的。图像仍然具有白色背景。我还将 XML 更新为如下所示:
<?xml version="1.0" encoding="utf-8"?>
<mycompany xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:orientation="horizontal">
<ImageButton
android:id="@+id/sectionDelete"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:background="@drawable/button_delete" />
<TextView
android:id="@+id/header"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/sectionDelete"
android:layout_toEndOf="@+id/sectionDelete"
android:layout_toLeftOf="@+id/sectionAdd"
android:text="test"
android:textAllCaps="true"
android:textColor="@color/navigation_bar"
android:textSize="16sp"
android:textStyle="bold" />
<ImageButton
android:id="@+id/sectionAdd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:background="@drawable/button_add" />
</RelativeLayout>
<View
android:id="@+id/line"
android:layout_marginTop="8dp"
android:layout_width="match_parent"
android:layout_height="2dp"
android:background="@android:color/darker_gray" />
</LinearLayout>
</mycompany>
【问题讨论】:
-
在 ImageButton 上使用 android:background="@null"
-
在图像查看器中打开图像时是否有白色背景?
-
在 Gimp 和 Paint 3d 中它显示它们是透明的。让同事使用 PhotoShop 进行验证。
-
已验证同事。他还清除了透明度并将其重新添加。结果相同。
标签: android transparency android-relativelayout android-imagebutton