【问题标题】:android: fill_parent of child view fills screenandroid:子视图的fill_parent填充屏幕
【发布时间】:2011-08-10 06:53:00
【问题描述】:

我正在尝试复制微调器控件(请不要问为什么)我正在努力使用分隔线。假微调器看起来不错,直到我将分隔线添加到图像视图的左侧。一旦我添加了分隔线,它的高度就会填满屏幕的剩余部分。有人可以解释一下吗?

以下xml:

.......

        <Spinner 
            android:id="@+id/equipment_spinner"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"/>
        <RelativeLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content">
            <Button
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_alignParentRight="true"/>
            <ImageView
                android:id="@+id/spinner_arrow"
                android:layout_width="45sp"
                android:layout_height="wrap_content"
                android:layout_alignParentRight="true"
                android:layout_centerVertical="true"
                android:src="@drawable/spinner_arrow"/>
        </RelativeLayout>
    </LinearLayout>    
</ScrollView>

产生以下屏幕:

添加分隔符后,xml 如下所示:

        <Spinner 
            android:id="@+id/equipment_spinner"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"/>
        <RelativeLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content">
            <Button
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_alignParentRight="true"/>
            <ImageView
                android:id="@+id/spinner_arrow"
                android:layout_width="45sp"
                android:layout_height="wrap_content"
                android:layout_alignParentRight="true"
                android:layout_centerVertical="true"
                android:src="@drawable/spinner_arrow"/>
            <View
                android:background="#e7ebe7"
                android:layout_width="1dip"
                android:layout_height="fill_parent"
                android:layout_toLeftOf="@id/spinner_arrow"/>
        </RelativeLayout>
    </LinearLayout>    
</ScrollView>

生成以下屏幕:

谁能发现我在这里做错了什么?...

【问题讨论】:

  • 我已对您的问题投了赞成票 - 如果其他人也这样做,您将有足够的代表来发布图片。

标签: android android-relativelayout fill-parent


【解决方案1】:

您应该为此使用九个补丁图像,而不是使用多个视图。这就是默认 Spinner 所做的。我不知道你为什么要创建一个新的微调器,但如果你保持视觉效果不变,你可以重复使用内置图像。

android.R.layout.simple_spinner_item 是一个可以重复使用的 TextView 布局。或者,您可以直接获取背景:android.R.drawable.btn_dropdown,这是一个 XML 选择器,可绘制每个状态的位图。 Android 源代码中提供了更多详细信息。

【讨论】:

  • 好建议...我不久前使用了选择器并忘记了这种方法..有时您会朝着正确的方向前进而忘记以不同的方式思考...感谢重定向...顺便说一句,我这样做是为了将一个多选微调器拼接在一起......几乎就在那里......再次感谢
【解决方案2】:

您已将高度设置为填充父级,该高度填充了相对布局的其余部分。您是否尝试将 View 放在按钮或 imageview 中,如下所示:

<Button
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true">
        <View
            android:background="#e7ebe7"
            android:layout_width="1dip"
            android:layout_height="fill_parent"
            android:layout_toLeftOf="@id/spinner_arrow"/>
</Button>
        <ImageView
            android:id="@+id/spinner_arrow"
            android:layout_width="45sp"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:src="@drawable/spinner_arrow"/>
    </RelativeLayout>

<Button
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"/>
        <ImageView
            android:id="@+id/spinner_arrow"
            android:layout_width="45sp"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:src="@drawable/spinner_arrow">
            <View
            android:background="#e7ebe7"
            android:layout_width="1dip"
            android:layout_height="fill_parent"
            android:layout_toLeftOf="@id/spinner_arrow"/>
</ImageView>
    </RelativeLayout>

如果这不起作用,请尝试将相对布局的高度设置为固定量,例如 10 dip 或微调器的大小……您可能需要尝试不同的大小,只是不要将相对布局保留为包装内容

【讨论】:

  • 它是一条单像素宽的线,应该从上到下延伸...宽度不是问题...它的高度是 fill_parent...我认为 fill_parent 应该只是扩展以填充直系父母容器
  • ...在这种情况下,它是未设置为 fill_parent 的相对布局
  • 我不认为将图像视图放在按钮中会膨胀...按钮不是视图组...我会尝试的。
  • 我不希望将布局设置为固定大小...随着布局调整大小,分隔线应延伸到底部...
  • 听起来我的两个建议中的第一个会更好
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多