2014-11-08 13 views
9

vui lòng tìm bên dưới bố cục của tôi & kết quả của nó; Tôi cần văn bản 'Tin nhắn văn bản' để căn chỉnh trên đường cơ sở của văn bản 'Tiêu đề' (Vui lòng tìm bên dưới mã và ảnh chụp nhanh). Sẽ rất vui nếu ai đó có thể cho tôi hướngCăn chỉnh đường cơ sở của hai chế độ xem văn bản trong Android

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="vertical" 
android:weightSum="1.0" 
android:baselineAligned="true" 
> 

<LinearLayout 
    android:orientation="horizontal" 
    android:layout_width="match_parent" 
    android:layout_height="0dp" 
    android:layout_weight="0.2" 
    android:background="@color/custom_orange" 
    android:gravity="center" 
    > 


    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="match_parent" 
     android:text="Header" 
     android:textSize="25sp" 
     android:id="@+id/title" 
     android:gravity="center" 
     android:background="@color/custom_red"/> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="match_parent" 
     android:text="Text message" 
     android:textSize="10sp" 
     android:id="@+id/language" 
     android:gravity="center" 
     android:background="@color/custom_green"/> 
</LinearLayout> 

</LinearLayout> 

Đây là kết quả

enter image description here

Đây là những gì tôi cần

enter image description here

Trả lời

18

Sử dụng RelativeLayout như một container cho TextView và thêm thuộc tính

android:layout_alignBaseline="@+id/title" 

để id/ngôn ngữ TextView

2

Chỉ cần sử dụng android: trọng lực = "bottom" trên LinearLayout (Các tuyến tính với TextView cả hai) và android: layout_height = "wrap_content" trong cả hai TextView.

+1

Không giải quyết câu hỏi của OP khi phông chữ có kích thước khác nhau. – javajon

+0

Thực tế là có. Tôi đã rất ngạc nhiên khi tôi vô tình tìm ra điều này. Bây giờ tôi đang cố gắng hiểu CÁCH. –

+3

Tôi biết điều này thực sự cũ, nhưng tôi sẽ chỉ để lại điều này vì tôi nghĩ nó hữu ích, LinearLayouts có một thuộc tính được gọi là "baselineAligned" (được bật theo mặc định), thực hiện chính xác tên của nó: Điều chỉnh các khung nhìn dựa trên đường cơ sở (khá nhiều mọi lớp con TextView) trừ khi chúng có trọng lực, đó là lý do tại sao bố cục của OP không hoạt động chính xác. Xem thêm tại đây: https://possiblemobile.com/2013/10/shifty-baseline-alignment/ –

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