2012-02-21 84 views
18

的权利我有一个布局对齐ImageView的布局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="fill_parent" 
    android:background="#FFFFFF"> 

    <TextView android:id="@+id/groupname" 
     android:paddingLeft="50px" 
     android:textSize="16px" 
     android:background="#FFFFFF" 
     android:textColor="#000000" 
     android:textStyle="normal" 
     android:layout_width="wrap_content" 
     android:layout_height="50px"/> 
    <ImageView 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_gravity="right" 
      android:layout_marginRight="6dp" 
      android:background="#ffffff" 
      android:src="@drawable/sort"/> 

</LinearLayout> 

Rendering like this presently I want to render like this

呈现在第一图像现在发生等,但我想呈现像在第二图像完全右对齐。任何帮助都是有用的。

期待您的回复。 谢谢。

+1

谁提到的RelativeLayout为相当多的解决方案的人有正确的解决方案。只是告诉我们不工作不会帮助你自己或任何人。 – Maurice 2012-02-21 08:47:33

+0

@Maurice谢谢,但对我来说,RelativeLayout的答案并不成立,这就是我所回答的。 – Mukunda 2012-02-21 09:08:24

回答

43
<?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="fill_parent" 
    android:background="#FFFFFF"> 

    <TextView android:id="@+id/groupname" 
     android:paddingLeft="50dp" 
     android:textSize="16dp" 
     android:textColor="#000000" 
     android:textStyle="normal" 
     android:layout_width="fill_parent" 
     android:layout_height="50dp" 
     android:layout_weight="0.9" 
     android:background="@color/black"/> 
    <ImageView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_gravity="right" 
      android:background="#ffffff" 
      android:src="@drawable/checkin" 
      android:layout_weight="0.1"/> 

</LinearLayout> 

试试这个,希望这会有所帮助。

Prview1 enter image description here

+0

谢谢。完美地工作。 – Mukunda 2012-02-21 09:16:45

+0

如果imageview具有固定宽度,则也可以在imageview上使用layout_weight。只需在textview上设置值为1的layout_weight即可。 – 2017-01-13 17:16:14

11

使用RelativeLayout作为父布局。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
       android:layout_width="fill_parent" 
       android:layout_height="fill_parent"> 

    <TextView android:id="@+id/groupname" 
       android:paddingLeft="50px" 
       android:textSize="16px" 
       android:textColor="#000000" 
       android:textStyle="normal" 
       android:layout_width="wrap_content" 
       android:layout_height="50px"/> 

    <ImageView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignParentRight="true" 
      android:layout_marginRight="6dp" 
      android:src="@drawable/ncc"/> 

</RelativeLayout> 
+1

感谢不起作用。 – Mukunda 2012-02-21 07:59:50

3
<?xml version="1.0" encoding="utf-8"?> 

<TextView android:id="@+id/groupname" 
    android:paddingLeft="50px" 
    android:textSize="16px" 
    android:background="#FFFFFF" 
    android:textColor="#000000" 
    android:textStyle="normal" 
    android:layout_width="wrap_content" 
    android:layout_height="50px"/> 
<ImageView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_gravity="right" 
     android:layout_marginRight="6dp" 
     android:background="#ffffff" 
     android:layout_alignParentRight="true" 
     android:src="@drawable/sort"/> 

+0

谢谢,但这是行不通的。 – Mukunda 2012-02-21 07:50:52

0

要么使用的RelativeLayout和TextView中的设置属性如下:

layout_width=fill_parent 
layout_height=wrap_content 
left_of="@+id/imgView" 

并ImageView的

layout_width=wrap_content 
layout_height=wrap_content 
alignparentright=true 
+0

感谢不起作用。 – Mukunda 2012-02-21 08:00:07

23

采用Android:layout_weight = “1” 为文本视图

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="horizontal" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:background="#FFFFFF"> 

    <TextView android:id="@+id/groupname" 
    android:paddingLeft="50px" 
    android:textSize="16px" 
    android:layout_weight="1" 
    android:background="#FFFFFF" 
    android:textColor="#000000" 
    android:textStyle="normal" 
    android:layout_width="0dp" 
    android:layout_height="50px"/> 
<ImageView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="right" 
     android:layout_marginRight="6dp" 
     android:background="#ffffff" 
     android:src="@drawable/sort"/> 

</LinearLayout> 
+0

谢谢,这很好! – AVEbrahimi 2013-02-03 11:21:24

+0

完美的解决方案! – Surekha 2017-11-09 19:13:52

0

你应该在XML中添加这样的事情......你想要右对齐对象...

android:layout_alignParentRight="true" 
android:layout_marginRight="..dp" 
+0

谢谢,但这不会是可能的,因为你将如何为不同的屏幕设置密度像素。 – Mukunda 2012-02-21 08:01:13

+0

通过创建文件夹/大,/ xlarge,/小布局为不同的屏幕... – timonvlad 2012-02-21 08:16:24

3

尝试使用RelativeLayout。

<?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="fill_parent" 
    android:background="#FFFFFF"> 

    <TextView 
     android:id="@+id/groupname" 
     android:layout_width="wrap_content" 
     android:layout_height="50px" 
     android:background="#FFFFFF" 
     android:paddingLeft="50px" 
     android:textColor="#000000" 
     android:textSize="16px" 
     android:textStyle="normal" /> 

    <ImageView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentRight="true" 
     android:background="#ffffff" 
     android:src="@drawable/ic_launcher" /> 

</RelativeLayout> 
+0

谢谢这也是行不通的。 – Mukunda 2012-02-21 07:59:30