2013-05-30 64 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"

相關問題