user806821
user806821

Reputation: 37

android: fill_parent of child view fills screen

I'm trying to replicate a spinner control (please don't ask why) I'm struggling with the divider. The fake spinner looks fine until I add the divider to the left of the imageview. Once I add the divider it's height fills the remaining portion of the screen. Can someone please explain this?

The following 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>

produces the following screen:

enter image description here

once I add the divider, the xml looks like this:

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

which produces the following screen:

enter image description here enter image description here can anyone spot what i'm doing wrong here?...

Upvotes: 3

Views: 904

Answers (2)

blazeroni
blazeroni

Reputation: 8350

You should use a nine-patch image for this instead of using multiple views. That's what the default Spinner does. I don't know why you want to create a new spinner, but if you're keeping the visuals the same you can just reuse the built-in images.

android.R.layout.simple_spinner_item is a TextView layout you could reuse. Or, you can just take the background directly: android.R.drawable.btn_dropdown, which is an XML selector drawable with bitmaps for each state. More details are available in the Android source code.

Upvotes: 2

Rob
Rob

Reputation: 1152

You have the height set to fill parent which fills the remainder of the Relative Layout. Have you tried putting the View inside the button or imageview as follows:

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

OR

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

If this doesnt work try setting the height of the relative layout to a fixed amount like 10 dip or however big that spinner is... you may have to try a fiew different sized, just dont leave the relativelayout as wrap content

Upvotes: 1

Related Questions