Creating two LinearLayouts has 50% of the screen without using layout_weight

Currently, I am doing a special xml-layout of the landscape screen, in which there are 4 images, and 2 LinearLayouts are next to each other in 2 images. These LinearLayouts I call linearLayout1 and linearLayout2 .

linearLayout1 marked with a blue rectangle:

linearLayout1

linearLayout2 marked with a blue rectangle:

linearLayout2

As you can see, the first uses ~ 80% of the screen, and the second uses what is left. I do not want this, of course, I want 50% for each. I cannot use layout_weight because it is already used in LinearLayouts (the location of two images) and nested weights are bad for performance.

I tried many different options, but I just can't get two LinearLayouts to have 50% of the screen. Here is the code:

 <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/db1_root" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="horizontal" > <LinearLayout android:id="@+id/title_container" style="@style/TitleBar" > <ImageView style="@style/TitleBarLogo" android:contentDescription="@string/imgViewDesc" android:src="@drawable/title_logo" /> <ImageView style="@style/TitleBarSeparator" android:contentDescription="@string/imgViewDesc" /> <TextView style="@style/TitleBarText" /> <ImageButton style="@style/TitleBarAction" android:contentDescription="@string/imgViewDesc" android:onClick="onClickAbout" android:src="@drawable/title_about" /> </LinearLayout> <RelativeLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_below="@id/title_container" android:layout_above="@+id/mobFoxView" > <!-- LEFT COLUMN --> <LinearLayout android:id="@+id/linearLayout1" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_above="@+id/mobFoxView" android:layout_alignParentLeft="true" android:layout_toLeftOf="@+id/linearLayout2" android:background="@color/white" android:gravity="center" android:orientation="vertical" android:weightSum="2" > <ImageView android:id="@+id/imgNews" android:layout_width="wrap_content" android:layout_height="0dp" android:layout_weight="1" android:contentDescription="@string/imgViewDesc" android:onClick="onClickFeature" android:src="@drawable/front_news_1" /> <ImageView android:id="@+id/imgReleases" android:layout_width="wrap_content" android:layout_height="0dp" android:layout_weight="1" android:contentDescription="@string/imgViewDesc" android:onClick="onClickFeature" android:src="@drawable/front_releases_1" /> </LinearLayout> <!-- RIGHT COLUMN --> <LinearLayout android:id="@+id/linearLayout2" android:layout_width="wrap_content" android:layout_height="fill_parent" android:layout_above="@+id/mobFoxView" android:layout_alignParentRight="true" android:layout_alignTop="@id/linearLayout1" android:background="@color/white" android:gravity="center" android:orientation="vertical" android:weightSum="2" > <ImageView android:id="@+id/imgArtists" android:layout_width="wrap_content" android:layout_height="0dp" android:layout_weight="1" android:contentDescription="@string/imgViewDesc" android:onClick="onClickFeature" android:src="@drawable/front_artists_1" /> <ImageView android:id="@+id/imgLabels" android:layout_width="wrap_content" android:layout_height="0dp" android:layout_weight="1" android:contentDescription="@string/imgViewDesc" android:onClick="onClickFeature" android:src="@drawable/front_labels_1" /> </LinearLayout> </RelativeLayout> <com.mobfox.sdk.MobFoxView android:id="@+id/mobFoxView" android:layout_width="fill_parent" android:layout_height="50dp" android:layout_alignParentBottom="true" mode="test" publisherId="@string/mobFoxID" /> </RelativeLayout> 
+6
source share
4 answers

Well, there are two options that I see here.

  • Screw this LINT warning and use nested weights anyway. Phones are fast, and this will lead to a difference in milliseconds, since you only inflate layouts once (most of the time). Having nested layouts is only bad for performance because the inflator needs to make more passes to measure layouts.

  • Translate the top LinearLayout using RelativeLayout and align the two children with an invisible View in the center like this:

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/top" android:layout_width="match_parent" android:layout_height="match_parent"> <LinearLayout android:id="@+id/center_point" android:layout_width="0dp" android:layout_height="0dp" android:layout_centerInParent="true"/> <LinearLayout android:id="@+id/left_layout" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_alignParentLeft="true" android:layout_alignRight="@+id/center_point"> </LinearLayout> <LinearLayout android:id="@+id/right_layout" android:orientation="horizontal" //default android:layout_width="match_parent" android:layout_height="match_parent" android:layout_alignParentRight="true" android:layout_alignLeft="@+id/center_point"> </LinearLayout> </RelativeLayout> 
+21
source

You can set "Weight" for layouts, for example:

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" > <LinearLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="1"> </LinearLayout> <LinearLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="1"> </LinearLayout> 

Thus, each line output occupies 50% of the screen. :)

+3
source

If you are trying to avoid using the function for its intended purpose, you probably should consider whether your approach is suitable ... It seems that you are trying to arrange the elements in rows and columns. Try using TableLayout .

0
source

Without layout_weight, put both of these linear passes into the parent linearlayout

 <LinearLayout orientation:horizontal> <LinearLayout Child 1 /> <LinearLayout Child 2 /> </LinearLayout> 

center them using layout_gravity, if the contents of these line outputs are the same size, they should be the same size

if not then you may have a problem

and you may need to resize using code

0
source

Source: https://habr.com/ru/post/915426/


All Articles