Can someone please tell me why my ImageButton is not showing when I use this layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/main"
android:orientation="vertical" >
<ImageButton
android:id="@+id/continuebutton"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1.02"
android:background="@android:color/transparent"
android:src="@drawable/continuebutton"
android:visibility="visible" />
</RelativeLayout>
When I use a LinearLayout, it shows up just fine. Can anyone please clarify what I am doing wrong?
You are trying to add layout_weight to a RelativeLayout – which doesn’t make any sense. Relative layout places items relative to one another. They don’t have their widths and heights calculated as a whole like what the LinearLayout does, because the flow matters on a Linear layout and not in a Relative layout.
Set the button to either wrap_content or match_parent. If you want weights, nest a LinearLayout inside the RelativeLayout.
Extra info on that here: Percentage width in a RelativeLayout
EDIT
LinearLayout looks at the weight AND 0dip values on its orientation when the calculations are made on how to layout its child views.. Say you have 2 items side by side in a horizontal linear-layout… if they are both 0dip width, the width will be calculated based on their weights. The RelativeLayout doesn’t do this. It doesn’t worry about the flow of items. It places items relative to one another’s placing.
HTH