3

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

以下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>

这会产生以下屏幕:

在此处输入图像描述 在此处输入图像描述 谁能发现我在这里做错了什么?...

4

2 回答 2

2

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

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

于 2011-08-09T22:53:00.933 回答
1

您将高度设置为填充父级,填充相对布局的其余部分。您是否尝试过将视图放入按钮或图像视图中,如下所示:

<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 或微调器的大小……您可能需要尝试不同的大小,只是不要将相对布局保留为包装内容

于 2011-08-09T22:14:05.987 回答