minHeight 不适用于线性布局中的 weight=“1" [英] minHeight not working with weight=“1” in Linearlayout

查看:21
本文介绍了minHeight 不适用于线性布局中的 weight=“1"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果屏幕尺寸很小,我想要一个可以具有最小高度或 wrap_content 的视图,如果屏幕尺寸很大,则占据其余空间.此视图是其父视图的中间子视图.

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

目前,我在 LinearLayout 中使用权重.它在大屏幕上运行良好,但在小屏幕上不显示视图.

Currently, I am using weight with LinearLayout. It's working fine with large screen but on a small screen its not showing the view.

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

请推荐.

推荐答案

我已经解决了这个问题.在 XML 文件中设置布局的权​​重1"(在我的case XML 与上面给出的相同),它会在 if Screen 的情况下工作尺寸很大,如果视图尺寸更小,还要检查语法比最小尺寸然后在语法上设置视图高度.样本代码:-

i had solve this problem. set weight "1" of layout in XML File(in my case XML is same as Given above), it will work in case of if Screen size is large and also check grammatically if size of view is less than a minimum size then set height of view grammatically. sample code:-

将 ViewTreeObserver 设置为查看:-

Set ViewTreeObserver to View:-

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

并在侦听器的回调中检查布局高度是否小于最小值,然后将高度设置为最小值:-

and in Listener's callback check if layout height is less than minimum then set 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);
            }

        }
    };

这篇关于minHeight 不适用于线性布局中的 weight=“1"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆