I have 2 LinearLayouts of Weight1,With 1st Layout,if i add anything it occupies full width instead of half of screen,How to avoid this
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:weightSum="2" tools:context="com.example.testapp1.MainActivity" > <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:background="@color/Aqua" android:orientation="vertical" android:weightSum="2" > <LinearLayout android:id="@+id/subview_1" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@color/Green" android:orientation="vertical" > <RelativeLayout android:id="@+id/insideLayout" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@color/Yellow" > <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@color/Aqua" android:orientation="vertical" > <View android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@color/DarkGray" /> </LinearLayout> </RelativeLayout> </LinearLayout> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:background="@color/BlueViolet" android:orientation="vertical" > </LinearLayout> </LinearLayout>
On adding view tag,it occupies full width instead of half as specified in weight1,What is problem i am making
Let's simplify your problem. The below should work. I recommend to imagine weight
as a percentage of 100 and so if you want half and half it is 50,50 (if you wanted a third each its 33,33,33). No need for the weightSum they have been removed. You were missing the layout_height
of 0dip
this is what allows the weight
to define the space taken.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.testapp1.MainActivity" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="50"
android:orientation="vertical">
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="50"
android:orientation="vertical" >
</LinearLayout>
</LinearLayout>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.