MinHeight does not work with weight = "1" in Linearlayout

I need a view that can have a minimum height or wrap_content if the screen size is small and the screen size is large, then take up the rest of the space. This view is the middle child of its parent.

I am currently using weight with LinearLayout. It works great with a large screen, but it is not visible on a small screen.

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:id="@+id/main_view" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@color/speaker_detail_background" android:clickable="true" android:paddingLeft="@dimen/padding_7dp" android:paddingRight="@dimen/padding_7dp" android:orientation="vertical" > <ImageView android:id="@+id/down_arrow_image_view" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_alignParentRight="true" android:padding="@dimen/padding_15dp" android:src="@drawable/down_arrow_icon" /> <RelativeLayout android:id="@+id/lytSpeakerDetails" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@id/down_arrow_image_view" android:minHeight="200dp" android:layout_weight="1" > <RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_centerInParent="true"> <RelativeLayout android:id="@+id/title_bar" android:layout_width="match_parent" android:layout_height="wrap_content" android:theme="@style/ThemeOverlay.AppCompat.Dark" android:visibility="visible" > <com.harman.jblconnectplus.ui.customviews.CustomFontTextView android:id="@+id/speaker_name_title_textview" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:background="@android:color/transparent" android:drawableLeft="@drawable/edit_name" android:drawablePadding="@dimen/padding_5dp" android:ellipsize="end" android:maxLength="16" android:text="@string/speaker_name" android:textColor="@android:color/white" android:textSize="@dimen/textSize_18sp" app:font="@string/font_name_opensans_semi" /> <com.harman.jblconnectplus.ui.customviews.CustomFontTextView android:id="@+id/setting_text_view" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/speaker_name_title_textview" android:layout_centerHorizontal="true" android:text="@string/setting" android:visibility="invisible" android:textColor="@color/white" android:textSize="14sp" app:font="@string/font_name_opensans_semi" /> </RelativeLayout> <LinearLayout android:id="@+id/lytSeekBar" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:layout_below="@id/title_bar" android:layout_marginLeft="@dimen/brightness_margin" android:layout_marginRight="@dimen/brightness_margin" android:visibility="visible"> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/darker" android:layout_gravity="center_vertical" /> <SeekBar android:layout_width="0dp" android:layout_weight="1" android:layout_height="wrap_content" android:id="@+id/seekBar" android:thumb="@drawable/knob" android:progressDrawable="@drawable/seekbar_progressbar" android:indeterminate="false" android:splitTrack="false" android:maxHeight="3dp" android:paddingLeft="10dp" android:paddingRight="10dp" android:progress="0" android:secondaryProgress="0" android:max="255" /> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/brighter" android:layout_gravity="center_vertical"/> </LinearLayout> </RelativeLayout> </RelativeLayout> <LinearLayout android:layout_width="match_parent" android:id="@+id/bottom_details_layout" android:layout_height="wrap_content" android:background="@drawable/round_border_white_bg" android:orientation="vertical" android:layout_marginTop="@dimen/margin_20dp" > <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:padding="@dimen/padding_5dp" android:gravity="bottom"> <include layout="@layout/speaker_detail_firmware_layout" /> </LinearLayout> <View android:layout_width="match_parent" android:layout_height="0.5dp" android:background="@color/divider_view" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <include layout="@layout/speaker_detail_voice_layout" /> </LinearLayout> </LinearLayout> </LinearLayout> 

Please offer.

0
android android-layout
source share
2 answers

I solved this problem. set the weight "1" of the layout in the XML file (in my case the XML is the same as above), it will work if the Screen size is large and also checked grammatically, if the size of the view is less than the minimum size, then set the height of the view grammatically . Sample Code: -

Install ViewTreeObserver to view: -

 lytSpeakerDetails = (RelativeLayout) view.findViewById(R.id.lytSpeakerDetails); ViewTreeObserver viewTreeObserver = lytSpeakerDetails.getViewTreeObserver(); viewTreeObserver.addOnGlobalLayoutListener(globalLayoutListener); 

and in the Listener callback, check if the minimum layout height is smaller, and then set the height to minimum: -

 ViewTreeObserver.OnGlobalLayoutListener globalLayoutListener = new ViewTreeObserver.OnGlobalLayoutListener() { @Override public void onGlobalLayout() { if (!isAdded()) return; lytSpeakerDetails.getViewTreeObserver().removeGlobalOnLayoutListener(this); int height = lytSpeakerDetails.getMeasuredHeight(); LinearLayout.LayoutParams imgViewParams = (LinearLayout.LayoutParams) lytSpeakerDetails.getLayoutParams(); /* imgViewParams.setMargins(0, (int) ( x * 1.5f) + (int) getResources().getDimension(R.dimen.card_shadow_y), 0, (int) x ); lytSpeakerDetails.setLayoutParams(imgViewParams);*/ if(height < (int)getResources().getDimension(R.dimen.brightness_minimum_height)) { LinearLayout.LayoutParams rel_btn = new LinearLayout.LayoutParams( ViewGroup.LayoutParams.WRAP_CONTENT, (int) getResources().getDimension(R.dimen.brightness_minimum_height)); lytSpeakerDetails.setLayoutParams(rel_btn); } } }; 
+1
source share

Your View limited. minHeight not guaranteed if the restriction of View limited. See the documentation.

Android: MinHeight

Defines the minimum height of the view. It is not guaranteed that the view will be able to reach this minimum height (for example, if its parent layout holds it back with a lower available height).

Perhaps ScrollView is something you can use, as it gives you virtually unlimited space. The question is what do you want when space becomes a problem and everything just doesn't fit.

0
source share

All Articles