在右角显示图像

时间:2015-01-13 13:06:57

标签: android

我有以下代码,我试图在标题栏的右角显示,但它会显示在中心。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="35dip"
    android:gravity="center_vertical"
    android:background="@drawable/formheader">

    <ImageView
        android:id="@+id/header"
        android:background="@drawable/ic_launcher"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="ShowRoom"
        android:textColor="@android:color/black"
        android:textStyle="bold"/>

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:clickable="true"
        android:background="@drawable/menu"/>

</LinearLayout>

3 个答案:

答案 0 :(得分:1)

这些是RelativeLayout的属性,因此它不能与LinearLayout一起使用。

android:layout_alignParentRight="true"
android:layout_alignParentTop="true"

android:layout_weight="1"添加到TextView

    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="ShowRoom"
        android:textColor="@android:color/black"
        android:textStyle="bold" />

更改如下:

<?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:gravity="center_vertical"
    android:orientation="horizontal" >

    <ImageView
        android:id="@+id/header"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/ic_launcher" />

    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:paddingLeft="5dp"
        android:paddingRight="5dp"
        android:text="ShowRoom"
        android:textColor="@android:color/black"
        android:textStyle="bold" />

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/ic_launcher"
        android:clickable="true" />

</LinearLayout>

答案 1 :(得分:0)

你可以通过两种方式实现,

  1. 如果您想继续LinearLayout,请关注@ prag的回答。

  2. 另一种最简单的方法是使用RelativeLayout,如下所示

    <RelativeLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="35dip"
        android:gravity="center_vertical"
        android:background="@drawable/formheader">
    
        <ImageView
            android:id="@+id/header"
            android:background="@drawable/ic_launcher"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_toRightOf="@id/header"
            android:layout_centerVertical="true"
            android:text="ShowRoom"
            android:textColor="@android:color/black"
            android:textStyle="bold"/>
    
        <ImageView
            android:id="@+id/imageView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:clickable="true"
            android:background="@drawable/ic_launcher"/>
    
    </RelativeLayout>
    

答案 2 :(得分:0)

试试这个布局

<?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:orientation="horizontal" >

    <ImageView
        android:id="@+id/header"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/ic_launcher" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:text="ShowRoom"
        android:textColor="@android:color/black"
        android:textStyle="bold" />

    <View
        android:layout_width="0dip"
        android:layout_height="wrap_content"
        android:layout_weight="1" >
    </View>

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="right"
        android:background="@drawable/ic_launcher"
        android:clickable="true" />

</LinearLayout>

注意:用你的替换我的可绘制图像。希望这可以解决您的问题

相关问题