简体   繁体   English

线性布局元素不占整个屏幕

[英]linear layout elements not taking up whole screen

I have a small linearlayout with another linearlayout that has some errors when i add weights to it and try for it to take up the whole screen. 我有一个小的线性布局与另一个线性布局,当我为它添加权重并试图占据整个屏幕时有一些错误。 Please check my code and help me out. 请检查我的代码并帮助我。

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"

    android:orientation="horizontal"

    android:id="@+id/linear_layout_two"
    android:layout_below="@+id/linear_layout_one"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true">

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

        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:textSize="25dp"
            android:textColor="#3193b9"
            android:text="99.3%"
            android:layout_weight="1"
            android:id="@+id/goals_accuracy"/>

        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:textSize="10dp"
            android:gravity="center"
            android:text="Goals accuracy"
            android:layout_weight="1"
            android:id="@+id/goals_accuracy_2"/>

    </LinearLayout>


    <ImageView
        android:layout_width="0dp"
        android:layout_height="60dp"
        android:src="@drawable/the_vertical"

        android:id="@+id/imageView8" />


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

        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:textSize="25dp"
            android:textColor="#3193b9"
            android:text="10:30hr"
            android:gravity="center"
            android:layout_weight="1"
            android:id="@+id/total_time_on_ice"/>

        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:textSize="10dp"
            android:text="Total time on ice"
            android:gravity="center"
            android:layout_weight="1"
            android:id="@+id/total_time_on_ice_2"/>

    </LinearLayout>
</LinearLayout>

I tried adding weights to it but it didnt work, so i removed it. 我尝试添加重量,但它没有工作,所以我删除它。 Although i do know that i have to set the width to 0 and set weights to 1 but I am confused on how to make it work on nested layouts. 虽然我知道我必须将宽度设置为0并将权重设置为1但我对如何使其在嵌套布局上工作感到困惑。 However, i was able to make work on a single linearlayout ... 但是,我能够在单个线性布局上进行工作......

Some notes: 一些说明:

  • Why are your ImageView elements 0 width? 为什么你的ImageView元素宽度为0?
  • The TextView elements having android:layout_weight="1" should have 0 height 具有android:layout_weight="1"的TextView元素应该具有0高度

It should solve your problem. 它应该解决你的问题。

Try this 尝试这个

<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:id="@+id/linear_layout_two"
        android:layout_below="@+id/linear_layout_one"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true">

    <LinearLayout
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="60dp"
            android:orientation="vertical"
            >

        <TextView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:textSize="25dp"
                android:textColor="#3193b9"
                android:text="99.3%"
                android:layout_weight="1"
                android:id="@+id/goals_accuracy"/>

        <TextView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:textSize="10dp"
                android:gravity="center"
                android:text="Goals accuracy"
                android:layout_weight="1"
                android:id="@+id/goals_accuracy_2"/>

    </LinearLayout>

    <LinearLayout
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="60dp"
            android:orientation="vertical">

        <TextView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:textSize="25dp"
                android:textColor="#3193b9"
                android:text="10:30hr"
                android:gravity="center"
                android:layout_weight="1"
                android:id="@+id/total_time_on_ice"/>

        <TextView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:textSize="10dp"
                android:text="Total time on ice"
                android:gravity="center"
                android:layout_weight="1"
                android:id="@+id/total_time_on_ice_2"/>

    </LinearLayout>
</LinearLayout>

I've dropped ImageView, add a background that render a right border to your fisrt LinearLayout 我已经删除了ImageView,添加了一个为你的fisrt LinearLayout渲染右边框的背景

Short explanation: 简短说明:

I have assigned to each LinearLayout an equal weight of 1. So they have half parent width each one. 我已经为每个LinearLayout分配了相等的权重1.因此它们每个都具有半父宽度。 And added width=0dp because you have an horizontal parent LinearLayout, so you must set to 0 sons width. 并且添加了width = 0dp,因为您有一个水平父LinearLayout,因此您必须设置为0个子宽度。

The ImageView instead is not useful, you can achieve border goal as I explained. 相反,ImageView没有用,你可以按照我的解释实现边界目标。 By this way you can also simplify your xml 通过这种方式,您还可以简化您的xml

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM