2012-02-21 29 views
18

Tôi có một bố tríAligning ImageView sang phải bố trí 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

Rendering đang xảy ra bây giờ như trong hình ảnh đầu tiên nhưng tôi muốn làm cho giống như trong hình ảnh thứ hai hoàn toàn đúng thẳng hàng. Mọi sự giúp đỡ đều có ích.

Mong trả lời của bạn. cảm ơn.

+1

Những người đề cập RelativeLayout như một giải pháp có khá nhiều giải pháp đúng. Chỉ cần nói với chúng tôi không làm việc là không giúp bản thân bạn hay bất cứ ai. – Maurice

+0

@Maurice cảm ơn, nhưng đối với tôi câu trả lời đã không làm việc ra cho RelativeLayout, đó là những gì tôi trả lời. – Mukunda

Trả lời

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> 

Hãy thử điều này, hy vọng điều này sẽ giúp.

Prview1 enter image description here

+0

cảm ơn. làm việc hoàn hảo. – Mukunda

+0

Điều này cũng có thể được thực hiện mà không có layout_weight trên số lần xem hình ảnh nếu số lần xem hình ảnh có chiều rộng cố định. Chỉ cần đặt layout_weight trên textview với giá trị là 1. –

11

Sử dụng RelativeLayout làm bố cục gốc.

<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

cảm ơn không hoạt động. – Mukunda

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

cảm ơn nhưng điều này không hoạt động. – Mukunda

0

Hoặc sử dụng RelativeLayout và thiết lập tài sản của TextView như sau:

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

và ImageView

layout_width=wrap_content 
layout_height=wrap_content 
alignparentright=true 
+0

cảm ơn không hoạt động. – Mukunda

23

sử dụng android: layout_weight = "1" cho xem văn bản

<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

cảm ơn, điều này là tốt! – AVEbrahimi

+0

Giải pháp hoàn hảo! – Surekha

0

bạn nên thêm điều như vậy trong xml ... đến đối tượng bạn muốn sắp xếp đúng ...

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

cảm ơn nhưng điều này sẽ không thể như thế nào bạn sẽ thiết lập pixel mật độ cho màn hình khác nhau. – Mukunda

+0

bằng cách tạo thư mục/lớn,/xlarge,/bố trí nhỏ cho các màn hình khác nhau ... – timonvlad

3

Hãy thử với 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

cảm ơn điều này cũng không hoạt động. – Mukunda

Các vấn đề liên quan