2010-01-22 43 views
11

Tôi có LinearLayout đơn giản với một TextView và một ImageView. Tôi muốn văn bản trong TextView được căn chỉnh về bên phải, nhưng kết quả cho thấy rằng Văn bản đã được căn chỉnh ở bên trái. Có gì sai với bố cục xml của tôi không? Cảm ơn.android: layout_gravity không hoạt động như mong đợi

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" android:layout_width="fill_parent" 
    android:layout_height="wrap_content" android:background="#E8E3E4"> 
    <LinearLayout android:orientation="horizontal" 
     android:layout_width="wrap_content" android:layout_height="wrap_content" 
     > 
     <TextView android:layout_width="260dp" android:layout_height="wrap_content" 
      android:text="More Comments" android:textStyle="bold" android:background="#ff0000" 
      android:textColor="#121222" android:layout_gravity="right" /> 
     <ImageView android:layout_width="50dp" 
      android:layout_height="wrap_content" android:src="@drawable/arrow_right" 
      android:layout_gravity="center" android:scaleType="centerInside" /> 
    </LinearLayout> 
</LinearLayout> 

Cảm ơn, Michael. Nhưng cách giải quyết mà bạn đề cập không hoạt động.

Tôi cố gắng để sử dụng RelativeLayout, nhưng nó vẫn đưa cho tôi kết quả như: Comments (Icon)

gì tôi mong đợi là Comments (Icon)

Các RelativeLayout xml dưới đây. Có gì sai với nó không?

<?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"> 
    <TextView android:id="@+id/label" android:layout_width="260dp" 
     android:layout_height="wrap_content" android:text="More Comments" 
     android:textStyle="bold" android:background="#ff0000" 
     android:textColor="#121222" android:layout_gravity="right" /> 
    <ImageView android:layout_width="50dp" android:layout_height="wrap_content" 
     android:src="@drawable/arrow_right" android:layout_gravity="center" 
     android:scaleType="centerInside" android:layout_toRightOf="@id/label" /> 
</RelativeLayout> 

Tiếp theo workaround trong bình luận của Michael, anh có chữ XML dưới đây. Tôi dự kiến ​​sản lượng là:

 RightLeft 

nhưng sản lượng thực tế là:

Right    Left 

Vì vậy, cách giải quyết không làm việc cho tôi. Có suy nghĩ gì không?

<LinearLayout android:orientation="horizontal" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content"> 
      <TextView 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_gravity="right" 
        android:layout_weight="1.0"      
        android:text="RIGHT"/> 
      <TextView 
        android:layout_width="wrap_content" 

        android:layout_height="wrap_content" 
        android:layout_gravity="left" 
        android:text="LEFT"/> 
    </LinearLayout> 

+0

Bạn có thể bao gồm xml của mình không? – jball

+0

Ông đã làm, ông chỉ không định dạng nó. –

+0

(cũng: được chỉnh sửa sao cho có thể nhìn thấy và hiển thị * dưới dạng mã * ...) –

Trả lời

7

Tôi đã tìm ra điều gì sai với xml của mình. Tôi nên sử dụng trọng lực thay vì layout_gravity để căn chỉnh văn bản trong TextView. XML sau hoạt động như mong đợi.

<?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"> 
    <TextView android:id="@+id/label" android:layout_width="260dp" 
     android:layout_height="wrap_content" android:text="More Comments" 
     android:textStyle="bold" android:background="#ff0000" 
     android:textColor="#121222" android:gravity="right" /> 
    <ImageView android:layout_width="50dp" android:layout_height="wrap_content" 
     android:src="@drawable/arrow_right" android:layout_gravity="center" 
     android:scaleType="centerInside" android:layout_toRightOf="@id/label" /> 
</RelativeLayout> 
+0

Sau đó, bạn nên đánh dấu câu trả lời này là 'được chấp nhận' (nhấp vào dấu chọn ở bên trái câu hỏi). –

1

tôi đã thực hiện chỉ có một vài dự án với android, nhưng android: layout_gravity xác định sự liên kết cho xem trong phụ huynh, android: lực hấp dẫn chỉ rõ sự liên kết của Số lượt xem có trong Chế độ xem có trọng số bạn chỉ định. Đảm bảo bạn đã chỉ định đúng quy định. Tài liệu không phân biệt giữa hai tài liệu này.

Sẽ dễ dàng hơn nếu bạn đăng một số mẫu XML.

CHỈNH SỬA:

Tôi thấy bạn đã đăng XML ngay bây giờ. Có vẻ như bạn đã thiết lập thuộc tính thích hợp, nhưng nếu bạn căn chỉnh văn bản ở bên phải, tại sao bạn đặt nó lần đầu tiên trong bố cục tuyến tính? Tôi đề nghị thay đổi nó để đi từ trái sang phải.

33

Tuyến tínhLọc với hướng nằm ngang không hỗ trợ lực hấp dẫn phải/trái. Bạn có thể tìm cách giải quyết here.

+0

+1, tôi đã dành chút thời gian để tìm thông tin này trước khi tôi thấy câu trả lời này. –

0

Đây là một số mã tôi đã viết cho dự án của mình. Nó hoàn thành những gì bạn đang tìm kiếm. Tôi chỉ cần thay đổi các loại điều khiển và như vậy và bạn nên tắt và chạy.

<RelativeLayout 
    android:layout_width="fill_parent" 
    android:layout_height="50dip" 
    android:background="#ffdddddd"> 
    <TextView android:layout_width="260dp" 
     android:layout_height="wrap_content" 
     android:text="More Comments" 
     android:textStyle="bold" 
     android:background="#ff0000" 
     android:textColor="#121222" 
     android:layout_centerHorizontal="true" 
     android:layout_alignParentTop="true" /> 
    <ImageView android:layout_width="50dp" 
     android:layout_height="wrap_content" 
     android:src="@drawable/arrow_right" 
     android:scaleType="centerInside" 
     android:layout_alignParentRight="true" 
     android:layout_alignParentTop="true" /> 
</RelativeLayout> 
+0

Xin chào Jared, tôi đã thử mã của bạn bằng cách thay thế ToogleButton bằng TextView. Nó không hoạt động: văn bản trong TextView không được căn chỉnh ở bên phải. – user256239

+0

Điều này sẽ làm việc chỉ cần sao chép nó hơn. – Jared

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