Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/212.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
为什么android:布局宽度=”;0px”;碎片?_Android_Android 3.0 Honeycomb - Fatal编程技术网

为什么android:布局宽度=”;0px”;碎片?

为什么android:布局宽度=”;0px”;碎片?,android,android-3.0-honeycomb,Android,Android 3.0 Honeycomb,在片段示例中,布局宽度始终为零。这个值的背后是什么 希望解释一下,请看一下《开发指南》中的for片段 如果你看左边的图片,它显示了手机如何显示初始活动a,当列表中的某个项目被选中时,该活动a将启动活动B 然而,右边显示的是如何将这两个活动同时显示为片段。注意片段A占据了屏幕的1/3,片段B占据了屏幕的2/3 现在来看同一篇开发指南文章中关于该布局的XML <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:and

在片段示例中,布局宽度始终为零。这个值的背后是什么

希望解释一下,请看一下《开发指南》中的for片段

如果你看左边的图片,它显示了手机如何显示初始活动a,当列表中的某个项目被选中时,该活动a将启动活动B

然而,右边显示的是如何将这两个活动同时显示为片段。注意片段A占据了屏幕的1/3,片段B占据了屏幕的2/3

现在来看同一篇开发指南文章中关于该布局的XML

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <fragment android:name="com.example.news.ArticleListFragment"
            android:id="@+id/list"
            android:layout_weight="1"
            android:layout_width="0dp"
            android:layout_height="match_parent" />
    <fragment android:name="com.example.news.ArticleReaderFragment"
            android:id="@+id/viewer"
            android:layout_weight="2"
            android:layout_width="0dp"
            android:layout_height="match_parent" />
</LinearLayout>

您可以看到两个片段的
layout\u width
为0dp,但它们也都有
layout\u weight
属性。第一个的权重为1,第二个的权重为2

简言之,当使用这样的布局时,将“宽度”设置为0意味着您不想显式强制执行宽度,操作系统应该计算出相对宽度作为总重量的分数。换句话说,1+2=3(总重量),但第一个活动要求宽度为屏幕宽度的1/总重量=1/3,片段B要求宽度为屏幕宽度的2/总宽度=2/3


现在假设您添加第三个片段,它的宽度也为0dp,重量为2。在这种情况下,总重量为1+2+2=5,第一个碎片的相对宽度为屏幕的1/5,其他两个碎片的相对宽度为屏幕的2/5或20%/40%/40%。

这对我来说很有效:

  • 对布局中的所有权重求和。在Squonk发布的示例中,有2个片段,总重量为3

  • 片段ArticleListFragment的权重为1,这意味着 尺寸为屏幕的1/3(3为总重量)

  • fragment ArticleReaderFragment的权重为2,这意味着大小将为screem的2/3


  • 希望有帮助。

    布局权重值也设置了吗?