【问题标题】:Add images to Custom Window Title Bar in Android将图像添加到 Android 中的自定义窗口标题栏
【发布时间】:2013-05-30 10:05:03
【问题描述】:
我需要在我的自定义窗口标题栏中添加 3 张图片。第一张图片的gravity 是left。第二张图片的gravity 是center,第三张图片的gravity 是right。我使用了下面的代码。但第三张图片没有显示。我认为它被第二张图片覆盖了。
如何在上述位置显示 3 张图片?
<?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="35dip"
android:background="#323331"
android:gravity="center_vertical"
android:orientation="horizontal"
android:paddingLeft="5dip" >
<ImageView
android:id="@+id/header_left"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="@string/header_img_dec"
android:src="@drawable/left_logo" />
<ImageView
android:id="@+id/header_middle"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:contentDescription="@string/header_img_dec"
android:gravity="center" />
<ImageView
android:id="@+id/header_right"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="right"
android:contentDescription="@string/header_img_dec"
android:src="@drawable/right_img" />
</LinearLayout>
【问题讨论】:
标签:
android
imageview
android-imageview
custom-titlebar
【解决方案1】:
将android:layout_width="fill_parent" 更改为android:layout_width="wrap_content"
【解决方案2】:
为此使用布局权重,还为父级LinearLayout设置android:layout_gravity="center_horizontal"
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="35dip"
android:background="#323331"
android:gravity="center_vertical"
android:orientation="horizontal"
android:layout_gravity="center_horizontal"
android:paddingLeft="5dip" >
<ImageView
android:id="@+id/header_left"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="@string/header_img_dec"
android:src="@drawable/left_logo"
android:layout_weight="1"
/>
<ImageView
android:id="@+id/header_middle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="@string/header_img_dec"
android:layout_weight="1" />
<ImageView
android:id="@+id/header_right"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:contentDescription="@string/header_img_dec"
android:src="@drawable/right_img" />
</LinearLayout>
【解决方案3】:
试试这个方法
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="35dip"
android:background="#323331"
android:gravity="center_vertical"
android:orientation="horizontal"
android:paddingLeft="5dip" >
<ImageView
android:id="@+id/header_left"
android:layout_alignParentLeft="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="@string/header_img_dec"
android:src="@drawable/left_logo" />
<ImageView
android:id="@+id/header_right"
android:layout_width="wrap_content"
android:layout_centerInParent="true"
android:layout_height="wrap_content"
android:contentDescription="@string/header_img_dec" />
<ImageView
android:id="@+id/header_middle"
android:layout_width="wrap_content"
android:layout_alignParentRight="true"
android:layout_height="wrap_content"
android:contentDescription="@string/header_img_dec"
android:src="@drawable/right_img" />
</RelativeLayout>
【解决方案4】:
在第二个 imageView 中它错过了 android:src= 并且您使用了 android:layout_width="fill_parent"。所以请改用 android:layout_width="wrap_content" 否则图像将填满容器的整个空间。
要将图像相互排列,我建议您使用RelativeLayout
试试这个:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/RelativeLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#323331"
>
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:src="@drawable/ic_launcher" />
<ImageView
android:id="@+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerVertical="true"
android:layout_marginLeft="82dp"
android:layout_toRightOf="@+id/imageView1"
android:src="@drawable/ic_launcher" />
<ImageView
android:id="@+id/imageView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:src="@drawable/ic_launcher" />
</RelativeLayout>