首页 文章

如何根据父视图的尺寸调整Android视图的大小

提问于
浏览
98

如何根据其父布局的大小调整视图大小 . 例如,我有一个填充全屏的 RelativeLayout ,我想要一个子视图,比如说 ImageView ,占据整个高度,宽度的1/2?

我已经尝试过所有 onMeasureonLayoutonSizeChanged 等等,我无法让它工作......

10 回答

  • 9

    我没有't know if anyone is still reading this thread or not, but Jeff'的解决方案只会让你到达那里(有点字面) . 他的onMeasure将做的是在父母的一半中显示一半的图像 . 问题是在 setMeasuredDimension 之前调用super.onMeasure将根据原始大小测量视图中的所有子项,然后在 setMeasuredDimension 调整大小时将视图切成两半 .

    相反,您需要调用 setMeasuredDimension (根据onMeasure覆盖的需要)并为您的视图提供新的 LayoutParams ,然后调用 super.onMeasure . 请记住,您的 LayoutParams 源自您的视图's parent type, not your view' s类型 .

    @Override 
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec){
       int parentWidth = MeasureSpec.getSize(widthMeasureSpec);
       int parentHeight = MeasureSpec.getSize(heightMeasureSpec);
       this.setMeasuredDimension(parentWidth/2, parentHeight);
       this.setLayoutParams(new *ParentLayoutType*.LayoutParams(parentWidth/2,parentHeight));
       super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }
    

    我相信你唯一一次遇到父亲问题 LayoutParams 如果父母是 AbsoluteLayout (已被弃用但有时仍然有用) .

  • 1

    您可以通过创建自定义视图并覆盖onMeasure()方法来解决此问题 . 如果始终在xml中对layout_width使用“fill_parent”,则传递给onMeasusre()方法的widthMeasureSpec参数应包含父级的宽度 .

    public class MyCustomView extends TextView {
    
        public MyCustomView(Context context, AttributeSet attrs) {
            super(context, attrs);
        }
    
        @Override
        protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
            super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    
            int parentWidth = MeasureSpec.getSize(widthMeasureSpec);
            int parentHeight = MeasureSpec.getSize(heightMeasureSpec);
            this.setMeasuredDimension(parentWidth / 2, parentHeight);
        }
    }
    

    您的XML看起来像这样:

    <LinearLayout 
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <view
            class="com.company.MyCustomView"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    </LinearLayout>
    
  • 25

    我最好不要自己设置测量尺寸 . 有's actually a bit of negotiation that goes on between the parent and child views and you don' t想重写所有code .

    但是,你可以做的是修改measureSpecs,然后用它们调用super . 您的视图永远不会知道它从其父级获得修改后的消息,并将为您处理所有事情:

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int parentHeight = MeasureSpec.getSize(heightMeasureSpec);
        int myWidth = (int) (parentHeight * 0.5);
        super.onMeasure(MeasureSpec.makeMeasureSpec(myWidth, MeasureSpec.EXACTLY), heightMeasureSpec);
    }
    
  • 122

    在XML上定义布局和视图时,可以指定视图的布局宽度和高度,以包装内容或填充父级 . 占据该区域的一半有点困难,但如果你想要另外一半的东西,你可以做类似下面的事情 .

    <LinearLayout android:layout_width="fill_parent"
                    android:layout_height="fill_parent">
            <ImageView android:layout_height="fill_parent"
                     android:layout_width="0dp"
                     android:layout_weight="1"/>
            <ImageView android:layout_height="fill_parent"
                     android:layout_width="0dp"
                     android:layout_weight="1"/>
       </LinearLayout>
    

    赋予两个相同重量的东西意味着它们会拉伸以占据相同比例的屏幕 . 有关布局的更多信息,请参阅the dev docs.

  • 15

    我相信Mayras XML-approach可以整齐 . 但是,只有通过设置weightSum,才能使其更精确 . 我不会再称之为黑客,但在我看来,最简单的方法是:

    <LinearLayout android:layout_width="fill_parent"
                  android:layout_height="fill_parent"
                  android:weightSum="1">
        <ImageView android:layout_height="fill_parent"
                   android:layout_width="0dp"
                   android:layout_weight="0.5"/>
    </LinearLayout>
    

    像这样你可以使用任何重量,例如0.6(和居中)是我喜欢用于按钮的重量 .

  • 3

    试试这个

    int parentWidth = ((parentViewType)childView.getParent()).getWidth();
    int parentHeight = ((parentViewType)childView.getParent()).getHeight();
    

    然后你可以使用LinearLayout.LayoutParams来设置chileView的参数

    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(childWidth,childLength);
    childView.setLayoutParams(params);
    
  • 2

    您现在可以使用PercentRelativeLayout . 繁荣!问题解决了 .

  • 0

    罗马,如果你想用Java代码(ViewGroup后代)做你的布局,那就有可能 . 诀窍是你必须同时实现onMeasure和onLayout方法 . 首先调用onMeasure,然后你需要“测量”子视图(有效地将其调整到所需的值) . 您需要在onLayout调用中再次调整大小 . 如果您未能执行此序列或未能在onMeasure代码的末尾调用setMeasuredDimension(),则无法获得结果 . 为什么这种复杂而脆弱的设计方式超出了我的范畴 .

  • 38

    如果对方是空的 . 我想最简单的方法是添加一个空视图(例如线性布局),然后将两个视图的宽度设置为fill_parent,并将它们的权重设置为1 .

    这适用于LinearLayout ...

  • 1

    它是这样的:

    <RelativeLayout 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"
        tools:context="com.company.myapp.ActivityOrFragment"
        android:id="@+id/activity_or_fragment">
    
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/linearLayoutFirst"
        android:weightSum="100">   <!-- Overall weights sum of children elements -->
    
        <Spinner
            android:layout_width="0dp"   <!-- It's 0dp because is determined by android:layout_weight -->
            android:layout_weight="50"
            android:layout_height="wrap_content"
            android:id="@+id/spinner" />
    
    </LinearLayout>
    
    
    <LinearLayout
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:layout_below="@+id/linearLayoutFirst"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:id="@+id/linearLayoutSecond">
    
        <EditText
            android:layout_height="wrap_content"
            android:layout_width="0dp"
            android:layout_weight="75"
            android:inputType="numberDecimal"
            android:id="@+id/input" />
    
        <TextView
            android:layout_height="wrap_content"
            android:layout_width="0dp"
            android:layout_weight="25"
            android:id="@+id/result"/>
    
    </LinearLayout>
    </RelativeLayout>
    

相关问题