简体   繁体   English

在右上角显示图像

[英]display the image in the right corner

I have the following code with which i am trying to display at right corner of the title bar but its getting displayed in center. 我有以下代码,试图将其显示在标题栏的右上角,但其显示在中间。

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

These are the properties of RelativeLayout , So it won't work with LinearLayout . 这些是RelativeLayout的属性,因此不能与LinearLayout

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

Add android:layout_weight="1" to TextView . 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" />

Change it like below : 如下更改:

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

You can do it in 2 ways, 您可以通过2种方式来做到这一点,

  1. Follow @prag's answer if you want to continue with LinearLayout . 如果要继续使用LinearLayout请遵循@prag的回答。

  2. Another simplest way is to use RelativeLayout like below, 另一个最简单的方法是使用如下所示的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> 

Try this layout 试试这个布局

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

Note: Replace my drawable image with yours. 注意:将我的可绘制图像替换为您的图像。 Hope this resolves your problem 希望这能解决您的问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM