简体   繁体   English

嵌套权重Android布局

[英]Nested weights Android layout

What i want to achive is to have a TextView, that takes about 70% of the screen, and the rest of the 30 % is divided EQUALY between 2 buttons set side by side (horizontally). 我想要实现的是拥有一个TextView,它占据了屏幕的70%左右,其余30%则被并排(水平)设置在2个按钮之间。 The only way I achieved this was using the code below...however the xml editor complains about the nested wights...altough it works I am understanding that this is not good practice...How should i do it instead ? 我实现此目标的唯一方法是使用下面的代码...但是xml编辑器抱怨嵌套的wights ...虽然可以正常工作,但我了解这不是一个好习惯...我应该怎么做呢?

<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"
android:orientation="vertical" >

<TextView android:id="@+id/news"
      android:textSize="14sp"
      android:textStyle="bold"
      android:textColor="#0033CC"
      android:layout_height="0px"
      android:layout_width="fill_parent"
      android:layout_weight="70"
      android:gravity="top"
/>

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="0px"
    android:orientation="horizontal"
    android:layout_weight="30">
    <Button
        android:id="@+id/new_order_button"
        android:layout_width="0px"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="@string/new_order" 
    />

    <Button
        android:id="@+id/previous_orders_button"
        android:layout_width="0px"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="@string/previous_orders" 
    />
</LinearLayout >

EDIT : UPDATED CODE...warning still present 编辑:更新的代码...警告仍然存在

Thanks :) 谢谢 :)

Actually I think the editor is complaining because having nested weights it's not efficient. 实际上,我认为编辑器在抱怨,因为权重嵌套效率不高。 Layout weights require a widget to be measured twice and if you have nested weights this is increased exponencially. 布局权重要求对小部件进行两次测量,如果您具有嵌套权重,则权重将呈指数增加。

You are receiving the warning from Lint because computing nested weights is expensive, and the most nested portion must be computed before the next layer can be. 您正在从Lint收到警告,因为计算嵌套权重非常昂贵,并且必须在嵌套下一层之前计算出嵌套最多的部分。 This delays drawing the layout to the screen, and is bad for performance. 这会延迟将布局绘制到屏幕上,并且对性能不利。

You can get around this by changing your layout design. 您可以通过更改布局设计来解决此问题。 Rather than have the Button s on the bottom arbitrarily take up 30% of the screen, you can have them take up just the layout_height they need with wrap_content , or set them to a static height using #dp . 与其让底部的Button任意占据屏幕的30%,还不如让它们仅用wrap_content占据它们所需的layout_height ,或者使用#dp将它们设置为静态高度。 The TextView at the top will then take up the rest of the top portion of the screen. 然后,顶部的TextView将占据屏幕顶部的其余部分。 This is easier for Android to draw, as there aren't any nested weights to compute. 由于没有任何嵌套的权重可以计算,因此Android绘制起来更容易。 Your Lint warning should go away: 您的Lint警告应消失:

<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"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/news"
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:gravity="top"
        android:textColor="#0033CC"
        android:textSize="14sp"
        android:textStyle="bold" />

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

        <Button
            android:id="@+id/new_order_button"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="50"
            android:text="@string/new_order" />

        <Button
            android:id="@+id/previous_orders_button"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="50"
            android:text="@string/previous_orders" />
    </LinearLayout>

as a best practice, you should not change size (height) of controls (in this case buttons) neither absolute nor relative - android will make it for you. 最佳做法是,不要更改绝对(绝对)或相对(相对)控件(在这种情况下为按钮)的大小(高度)-android会帮你做到这一点。 In your case I simply would create a FlowLayout as the root container and then put the LinearLayout with two buttons (yielding 50% of space each) at the bottom. 在您的情况下,我只需创建一个FlowLayout作为根容器,然后在LinearLayout的底部放置两个按钮(每个按钮占用50%的空间)即可。 The TextView you can then layout to the top of the LinearLayout with the buttons occupying remaining space. 然后,您可以将TextView布局到LinearLayout的顶部,而按钮会占用剩余空间。 However, you still can nest weighted layouts but its suboptimal to the performance of rendering. 但是,您仍然可以嵌套加权布局,但是它对渲染性能而言不是最优的。

One other possibility is to use a LinearLayout with 2 TableRows inside and then distribute the weights to the TableRows like so: 另一种可能性是使用内部带有2个TableRowsLinearLayout ,然后将权重分配给TableRows如下所示:

<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"
    android:orientation="vertical" >

    <TableRow
        android:id="@+id/tableRow1"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="70" >

        <TextView
            android:id="@+id/news"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:gravity="top"
            android:textColor="#0033CC"
            android:textSize="14sp"
            android:textStyle="bold" />
    </TableRow>

    <TableRow
        android:id="@+id/tableRow2"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="30" >

        <Button
            android:id="@+id/new_order_button"
            android:layout_width="0px"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Button1" />

        <Button
            android:id="@+id/previous_orders_button"
            android:layout_width="0px"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Button2" />
    </TableRow>

</LinearLayout>

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

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