2013-05-30 60 views
1

我需要在自定义窗口标题栏中添加3个图像。第一张图片的gravityleft。第二张图像的gravity为第三张图像的centergravityright。我使用了下面的代码。但不显示第三张图片。我认为它被第二张图片覆盖。将图像添加到Android中的自定义窗口标题栏

如何在上述位置显示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> 

回答

2

使用布局权重这个,还设置android:layout_gravity="center_horizontal"LinearLayout

<?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> 
0

尝试这种方式

<code>enter image description here</code>

<?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> 
0

在它错过Android的第二ImageView的: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> 
5

变化android:layout_width="fill_parent"android:layout_width="wrap_content"

相关问题