Android 嵌套布局很容易使用,为什么要使用其他布局?
每次我使用嵌套布局时,我的sweet ide都会显示一个错误,说“嵌套布局的性能很差”。因此,我挖掘了一点原因,发现这是指数计算的东西,等等。通常也建议相对线性布局使用布局。但是假设下面的代码Android 嵌套布局很容易使用,为什么要使用其他布局?,android,nested,android-linearlayout,android-relativelayout,Android,Nested,Android Linearlayout,Android Relativelayout,每次我使用嵌套布局时,我的sweet ide都会显示一个错误,说“嵌套布局的性能很差”。因此,我挖掘了一点原因,发现这是指数计算的东西,等等。通常也建议相对线性布局使用布局。但是假设下面的代码 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_pa
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent" android:background="#cccccc"
android:layout_height="match_parent" android:orientation="vertical"
tools:context=".MainActivity" android:weightSum="10">
<LinearLayout android:layout_marginTop="9dp" android:layout_marginLeft="10dp"
android:background="@drawable/sub_background_dashboard" android:layout_marginRight="10dp"
android:layout_marginBottom="3dp"
android:layout_width="match_parent" android:layout_weight="4"
android:layout_height="0dp">
</LinearLayout>
<LinearLayout
android:layout_width="match_parent" android:weightSum="2"
android:layout_weight="2.3"
android:layout_height="0dp" android:layout_marginTop="5dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp" android:layout_marginBottom="2dp">
<LinearLayout
android:layout_width="0dp" android:background="@drawable/sub_background_dashboard"
android:layout_weight="1" android:layout_marginRight="3.5dp"
android:layout_height="match_parent" >
</LinearLayout>
<LinearLayout
android:layout_width="0dp" android:background="@drawable/sub_background_dashboard"
android:layout_weight="1" android:layout_marginLeft="3.5dp"
android:layout_height="match_parent" >
</LinearLayout>
</LinearLayout>
<LinearLayout android:layout_marginTop="5dp" android:layout_marginLeft="10dp"
android:background="@drawable/sub_background_dashboard" android:layout_marginRight="10dp"
android:layout_marginBottom="3dp"
android:layout_width="match_parent" android:layout_weight="2.7"
android:layout_height="0dp">
</LinearLayout>
<LinearLayout
android:layout_width="match_parent" android:weightSum="10"
android:layout_weight="1"
android:layout_height="0dp" android:layout_marginTop="3dp"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent" android:weightSum="3"
android:layout_weight="9" android:background="#77000000"
android:layout_height="0dp"
android:orientation="horizontal">
<Button
android:layout_width="0dp" android:layout_weight="1" android:textColor="#DDDDDDDD"
android:layout_height="match_parent" android:background="#00000000"
android:gravity="center" android:textSize="17sp"
android:text="Snooze"/>
<View
android:layout_width="1dp" android:layout_marginTop="5dp"
android:layout_height="match_parent" android:layout_marginBottom="5dp"
android:background="#bbbbbbbb"/>
<Button
android:layout_width="0dp" android:layout_weight="1"
android:textSize="17sp" android:background="#00000000"
android:text="Dismiss" android:gravity="center"
android:layout_height="match_parent" android:textColor="#DDDDDDDD"/>
<View
android:layout_width="1dp" android:layout_marginTop="5dp"
android:layout_height="match_parent" android:layout_marginBottom="5dp"
android:background="#bbbbbbbb"/>
<Button
android:layout_width="0dp" android:layout_weight="1"
android:textSize="17sp" android:background="#00000000"
android:text="Cancel" android:gravity="center"
android:layout_height="match_parent" android:textColor="#DDDDDDDD"/>
</LinearLayout>
<View
android:layout_width="match_parent" android:background="#ff0000"
android:layout_height="0dp" android:layout_weight="1"/>
</LinearLayout>
</LinearLayout >
将线性布局视为视图等的模板。这行xml代码适用于纵向模式的每个屏幕大小!!!。计算已经完成,不需要我这边的百分比计算(以及它的实现),所有内容都像预期的那样覆盖了空间
如果我要使用相对布局,对于宽度计算要长得多的水平视图,可能需要动态侧(我不是建议在动态侧使用gui不好,当然更好),所以即使是这样的小型嵌套布局,是否仍然不明智?我也使用嵌套线性布局,因为它很容易。但是为了提高性能,您可以更好地使用GRIDLAYOUT。Android在冰淇淋三明治中引入了它,以避免嵌套布局 举个小例子:
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/GridLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:columnCount="2"
android:rowCount="7"
tools:context=".MainActivity" >
<ImageView
android:id="@+id/imageView1"
android:layout_column="0"
android:layout_row="0"
android:src="@drawable/ic_launcher" />
<TextView
android:id="@+id/textView1"
android:layout_column="1"
android:layout_gravity="right"
android:layout_row="0"
android:text="TextView" />
<View
android:id="@+id/view1"
android:layout_height="16dp"
android:layout_column="0"
android:layout_columnSpan="2"
android:layout_row="1"
android:background="#F90" />
<Button
android:id="@+id/button1"
android:layout_column="0"
android:layout_row="2"
android:text="Button1" />
<Button
android:id="@+id/button2"
android:layout_column="0"
android:layout_row="3"
android:text="Button2" />
<Button
android:id="@+id/button3"
android:layout_column="0"
android:layout_row="4"
android:text="Button3" />
<View
android:id="@+id/view2"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_column="0"
android:layout_gravity="fill_vertical|fill_horizontal"
android:layout_row="5"
android:background="#F90" />
<ImageView
android:id="@+id/imageView2"
android:layout_column="0"
android:layout_row="6"
android:src="@drawable/ic_launcher" />
<TextView
android:id="@+id/textView3"
android:layout_column="1"
android:layout_gravity="right"
android:layout_row="2"
android:text="HEADER" />
<View
android:id="@+id/view3"
android:layout_width="0dp"
android:layout_height="16dp"
android:layout_column="1"
android:layout_gravity="bottom|fill_horizontal"
android:layout_row="6"
android:background="#F90" />
“无需百分比”--您使用的是android:layout\u weight,这相当于一件事。我的意思是“无需从我这边计算百分比(以及实现百分比)”,每一行都有不同数量的小部件。。还可以吗?如果你在android studio上检查这一点,你会发现不同小部件大小的行看起来不像我的代码中那样好。所以我的答案是,是否有可能通过网格布局达到代码中的精确外观?复制该代码(将线性布局的背景更改为彩色#ff0000示例),并查看plsI将于明天制作并共享。谢谢,祝您度过愉快的一天(或夜晚)