2011-08-04 45 views
6

Tôi đang cố thêm Button vào LinearLayout sau TextView nhưng không hiển thị.Nút không hiển thị trong LinearLayout

Đây là mã bố trí của tôi

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/LinearLayout01" 
    android:layout_width="fill_parent" 
    android:layout_height="45dip"> 

    <TextView android:layout_width="fill_parent" 
     android:layout_height="45dip" 
     android:paddingLeft="5dip" 
     android:paddingRight="5dip" 
     android:textStyle="bold" 
     android:textSize="17dip" 
     android:gravity="center_vertical" 
     android:id="@+id/tvChild" 
     android:text="Children" 
     android:textColor="#ffCCCC22" /> 

    <Button android:id="@+id/submit" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:text="Submit" /> 
</LinearLayout> 

Các TextView được hiển thị một cách chính xác với văn bản thích hợp nhưng thay vì một Button Tôi nhận được một không gian trống lớn 3:57 chiều dài dòng.

Tôi đang thiếu gì?

+3

Thay đổi android: layout_width = "fill_parent" của TextView để android: layout_width = "wrap_content" Hoặc Trong thẻ LinearLayout bạn thêm android: orientation = "vertical" –

+0

Cảm ơn Kartik .. Nó hoạt động – Codemator

Trả lời

6

Hãy thử điều này.

<?xml version="1.0" encoding="utf-8"?> 
    <LinearLayout android:id="@+id/LinearLayout01" 
     android:layout_width="fill_parent" android:layout_height="45dip" 
     xmlns:android="http://schemas.android.com/apk/res/android"> 
     <TextView android:layout_width="wrap_content" 
      android:layout_height="45dip" android:paddingLeft="5dip" 
      android:paddingRight="5dip" android:textStyle="bold" android:textSize="17dip" 
      android:gravity="center_vertical" android:id="@+id/tvChild" 
      android:text="Children" android:textColor="#ffCCCC22" 
      /> 
      <Button android:id="@+id/submit" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_alignParentBottom="true" 
       android:text="Submit" /> 
    </LinearLayout> 

Sử dụng android:layout_width="wrap_content" thay vì android:layout_width="fill_parent" cho TextViewButton

+0

Hoàn hảo .. Nó đã giúp tôi rất nhiều .. – Codemator

+0

Ngay cả khi OP là mới bạn nên * không * yêu cầu cho một upvote * và * chấp nhận câu trả lời. – Reno

+1

Đọc những gì tôi đã viết: Ngay cả khi ông là người mới, bạn không nên làm điều đó. Bạn có thể nói với anh ấy * cách chấp nhận câu trả lời *. Không bắt buộc người dùng chấp nhận * câu trả lời * của bạn hoặc thậm chí là upvote nó. Câu trả lời của bạn không * giải thích * tại sao giải pháp của bạn hoạt động. [Đọc này] (http://meta.stackexchange.com/questions/95470/down-vote-code-only-answers) – Reno

4

Vấn đề là bạn đã đặt android:layout_width="fill_parent" cho số TextView, do đó, nó có chiều rộng toàn màn hình chỉ để hiển thị TextView. Và nó không thể hiển thị Button.

Dưới đây là hai lựa chọn cho bạn:

  1. Thêm TextViewButton trên một hàng duy nhất.

    Thay đổi thuộc tính layout_width của TextView thành wrap_content.

  2. Thêm TextViewButton theo chiều dọc.

    Thay đổi thuộc tính định hướng LinearLayout 's thành vertical.

+0

Chính xác .. Tôi hiểu rồi .. Cảm ơn thời gian quý báu của bạn – Codemator

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