簡體   English   中英

LinearLayout不顯示

[英]LinearLayout not Displaying

嗨,我正在學習Android。

這是我的布局的代碼:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView 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="com.example.android.aidsdruginformation.DetailActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        >
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:orientation="horizontal">

            <TextView
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:elevation="4dp"
                android:padding="8dp"
                android:text="ApprovalStatus"
                android:textAlignment="center" />

            <TextView
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:elevation="4dp"
                android:padding="8dp"
                android:text="FDA-approved"
                android:textAlignment="center" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:orientation="horizontal">

            <TextView
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:elevation="4dp"
                android:padding="8dp"
                android:text="ApprovalStatus"
                android:textAlignment="center" />

            <TextView
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:elevation="4dp"
                android:padding="8dp"
                android:text="GDA-approved"
                android:textAlignment="center" />
        </LinearLayout>
    </LinearLayout>
</ScrollView>

UI屏幕截圖:

截圖

我希望UI外觀如何: 使用者介面范例 為什么第二個LinearLayout不顯示? 難道我做錯了什么? 在使用layout_width ,父元素應具有固定尺寸嗎? 請指教..

鏈接到我的倉庫: https : //github.com/MukundPradeep/AidsDrugInformation

您可以使用android:fillViewport

定義滾動視圖是否應拉伸其內容以填充視口。

必須為布爾值,“ true”或“ false”。

您應該嘗試以下方法。 LinearLayout設為root。

    <LinearLayout
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <ScrollView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:fillViewport="true">
       // Your Staff
    </ScrollView>
</LinearLayout>

在此處輸入圖片說明

嘗試,為ScrollView添加一些屬性,

android:fillViewport =“ true”,android:isScrollContainer =“ true”

將LinearLayout添加為ScrollView的父級


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

    <ScrollView
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fillViewport="true"
        android:isScrollContainer="true" >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:weightSum="10" >

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:orientation="horizontal"
                android:weightSum="2" >

                <TextView
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    android:elevation="4dp"
                    android:gravity="center"
                    android:padding="8dp"
                    android:text="ApprovalStatus"
                    android:textAlignment="center" />

                <TextView
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    android:elevation="4dp"
                    android:gravity="center"
                    android:padding="8dp"
                    android:text="FDA-approved"
                    android:textAlignment="center" />
            </LinearLayout >

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:orientation="horizontal"
                android:weightSum="2" >

                <TextView
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    android:elevation="4dp"
                    android:gravity="center"
                    android:padding="8dp"
                    android:text="ApprovalStatus"
                    android:textAlignment="center" />

                <TextView
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    android:elevation="4dp"
                    android:gravity="center"
                    android:padding="8dp"
                    android:text="GDA-approved"
                    android:textAlignment="center" />
            </LinearLayout >

            <View
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="8" />
        </LinearLayout >
    </ScrollView >
</LinearLayout >

通過我的倉庫

我發現框架正在使用layout v-17文件夾下的activity_detail.xml ,因為我使用的android:textAlignment="center"屬性僅適用於API 17+。 我的目標SDK是23。因此,我發布的布局(在layout目錄下)僅會在運行Android版本低於17的設備上顯示。

如果您打算使用上述屬性,請確保在為不同API生成的所有布局文件夾中實現布局更改。

感謝大家為我提供幫助。

只需給父線性布局此屬性即可:

android:weightSum="2"那么您的兩個子級線性布局都會顯示在屏幕上。

我嘗試了您的代碼,它顯示了

嘗試這種方式

<?xml version="1.0" encoding="utf-8"?>
<ScrollView 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"
    android:fillViewport="true"
    tools:context="com.example.android.aidsdruginformation.DetailActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        >
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:orientation="horizontal">

            <TextView
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:elevation="4dp"
                android:padding="8dp"
                android:text="ApprovalStatus"
                android:textAlignment="center" />

            <TextView
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:elevation="4dp"
                android:padding="8dp"
                android:text="FDA-approved"
                android:textAlignment="center" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:orientation="horizontal">

            <TextView
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:elevation="4dp"
                android:padding="8dp"
                android:text="ApprovalStatus"
                android:textAlignment="center" />

            <TextView
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:elevation="4dp"
                android:padding="8dp"
                android:text="GDA-approved"
                android:textAlignment="center" />
        </LinearLayout>
    </LinearLayout>
</ScrollView>

OUTPUT
在此處輸入圖片說明

  <?xml version="1.0" encoding="utf-8"?>
<ScrollView 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="com.example.android.aidsdruginformation.DetailActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        >
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <TextView
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:elevation="4dp"
                android:padding="8dp"
                android:text="ApprovalStatus"
                android:textAlignment="center" />

            <TextView
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:elevation="4dp"
                android:padding="8dp"
                android:text="FDA-approved"
                android:textAlignment="center" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <TextView
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:elevation="4dp"
                android:padding="8dp"
                android:text="ApprovalStatus"
                android:textAlignment="center" />

            <TextView
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:elevation="4dp"
                android:padding="8dp"
                android:text="GDA-approved"
                android:textAlignment="center" />
        </LinearLayout>
    </LinearLayout>
</ScrollView>

您將第一個布局的權重設為1。這將占據整個屏幕高度。 您可以將其划分。 但是如果您賦予權重,那么還會在scrollview內部使用該scrollview的用途是什么。

檢查此內容。如果有幫助,請使其正確。

<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="20dp">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    >

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="10dp"
        android:layout_weight="1"
        android:id="@+id/l1"
        >


            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:paddingLeft="10dp"
                android:paddingRight="10dp"
                android:textStyle="bold"
                android:textColor="#ffffff"
                android:text="Now book a ride at 6Rs/km with uber go"
                android:layout_centerVertical="true"
                android:layout_alignParentStart="true" />




            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:paddingLeft="10dp"
                android:paddingRight="10dp"
                android:text="7/11 supermarket get special discounts today only"
                android:textStyle="bold"
                android:textColor="#ffffff"
                android:layout_centerVertical="true"
                android:layout_alignParentStart="true" />






    </RelativeLayout>
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/l1"
        android:padding="10dp"
        android:layout_weight="1"
        android:id="@+id/l2">


            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:paddingLeft="10dp"
                android:paddingRight="10dp"
                android:textStyle="bold"
                android:textColor="#ffffff"
                android:text="10% discount on all meals"
                android:layout_centerVertical="true"
                android:layout_alignParentStart="true" />




            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:paddingLeft="10dp"
                android:paddingRight="10dp"
                android:textStyle="bold"
                android:textColor="#ffffff"
                android:text="Clearance sale upto 40% off"
                android:layout_centerVertical="true"
                android:layout_alignParentStart="true" />





    </RelativeLayout>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="201dp"
        android:layout_below="@id/l2"
        android:layout_weight="1"
        android:padding="10dp"
        >

       <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:paddingLeft="10dp"
                android:paddingRight="10dp"
                android:textStyle="bold"
                android:textColor="#ffffff"
                android:text="Starbucks coffee try our special new mocha latte for a limited period discount"
                android:layout_centerVertical="true"
                android:layout_alignParentStart="true" />

               <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:paddingLeft="10dp"
                android:paddingRight="10dp"
                android:textStyle="bold"
                android:textColor="#ffffff"
                android:text="Buy 2 get 1 free*"
                android:layout_centerVertical="true"
                android:layout_alignParentStart="true" />
     </RelativeLayout>
</LinearLayout>

將兩個線性布局與另一個具有垂直方向的線性布局包裝在一起(如許多注釋所示)。 在您的情況下,兩個線性布局都相互重疊,因此您只能看到一個。 除非它有足夠的項目來滾動,否則它將無法滾動。

暫無
暫無

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

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