[英]Add images to Custom Window Title Bar in Android
I need to add 3 images in my custom window title bar. 我需要在自定义窗口标题栏中添加3张图像。 gravity
of 1st image is left
. left
第一张图像的gravity
。 gravity
of 2nd image is center
and gravity
of 3rd image is right
. 第二个图像的gravity
为center
,第三个图像的gravity
为right
。 I used the below code. 我用下面的代码。 but 3rd image not displaying. 但没有显示第三张图片。 I think it is covered by 2nd image. 我认为它已被第二张图片覆盖。
How can I display 3 images in above positions ? 如何在上述位置显示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:layout_width="fill_parent"
更改为android:layout_width="wrap_content"
Use layout weights for this, also set android:layout_gravity="center_horizontal"
for parent LinearLayout
为此使用布局权重,还为父级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>
try this way 尝试这种方式
<?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>
In the second imageView it missed android:src= and you have used android:layout_width="fill_parent". 在第二个imageView中,它错过了android:src =,并且您使用了android:layout_width =“ fill_parent”。 so use instead android:layout_width="wrap_content" otherwise the image will fill the entire space of the container. 因此请改用android:layout_width =“ wrap_content”,否则图像将填充容器的整个空间。 To arrange the images with each other, I advise you to use a RelativeLayout 要相互排列图像,建议您使用RelativeLayout
Try this : 尝试这个 :
<?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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.