簡體   English   中英

按鈕未顯示在 LinearLayout 中

[英]Button not displaying in LinearLayout

我試圖在TextView之后的LinearLayout添加一個Button ,但它沒有顯示。

這是我的布局代碼

<?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>

TextView使用正確的文本正確顯示,但我得到了一個三到四行長的大空白空間,而不是一個Button

我錯過了什么?

問題是您為TextView設置了android:layout_width="fill_parent" ,因此它占用了全屏寬度來顯示TextView 它無法顯示Button

這里有兩個選項供您選擇:

  1. 在單行上添加TextViewButton

    TextViewlayout_width屬性更改為wrap_content

  2. 垂直添加TextViewButton

    LinearLayout的方向屬性更改為vertical

試試這個。

<?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>

TextViewButton使用android:layout_width="wrap_content"而不是android:layout_width="fill_parent"

你也可以嘗試類似的東西

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:orientation="vertical"
    android:layout_margin="10dp"

    >

    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="3"
        android:layout_gravity="center"
        android:text="Hello World!"/>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:layout_gravity="center"
        android:text="Submit"/>

</LinearLayout>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM