【问题标题】:fit size of parent layout适合父布局的大小
【发布时间】:2017-12-09 15:33:52
【问题描述】:

我有一个简单的线性布局,想将屏幕分成大小相等的两个部分。

在上面,我想放一个SurfaceView,它的大小与其父级完全相同,即屏幕的一半。不幸的是,它总是要么占用所有屏幕,要么什么都不占用(取决于配置)。 为什么会这样?我该如何更改?

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:weightSum="2"
    android:orientation="vertical">

    <LinearLayout android:id="@+id/layoutUpperDaw"
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="#192832">

        <com.example.MySurvfaceView
            android:id="@+id/id_mySurfaceView"
            android:layout_width="match_parent"

            android:layout_height="0pt"
            android:layout_weight="1"

            OR

            android:layout_height="match_parent"

            >

        </com.example.MySurvfaceView>

    </LinearLayout>

    <LinearLayout android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="#193222">

        ...
    </LinearLayout>
</LinearLayout>

【问题讨论】:

    标签: android android-layout layout android-linearlayout


    【解决方案1】:

    当您提供weight 时,您必须将身高或体重设置为 0dp,如下所示。

    <LinearLayout android:id="@+id/layoutUpperDaw"
            android:orientation="horizontal"
            android:layout_width="fill_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:background="#192832">
    

    <com.example.MySurvfaceView
                android:id="@+id/id_mySurfaceView"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                >
    

    更新:以上关于权重使用的答案是正确的,请确保在您使用权重的所有地方,高度或宽度都是 0dp。

    您实际上并不需要嵌套的 LinearLayout。以下应该工作。

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
    
    
            <com.example.MySurvfaceView
                android:id="@+id/id_mySurfaceView"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                >
    
            </com.example.MySurvfaceView>
    
    
    
        <LinearLayout android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:background="#193222">
    
    
        </LinearLayout>
    </LinearLayout>
    

    【讨论】:

    • 对我不起作用。此时MySurfaceView 不可见。
    • 好的,没有第一个嵌套内部 LinearLayout 的方法有效,但我不确定我是否了解 LinearLayout 背后的逻辑。当我在MySurvfaceView 周围添加LinearLayoutweight=1height=0 时,它不起作用。
    【解决方案2】:

    以编程方式:

    1. 找到父布局。

      GridLayout gridLayout =(GridLayout)findViewById(R.id.gridLayout);`

    2. 获取父级的测量高度和宽度。

      int height = gridLayout.measuredHeight();

      int width = gridLayout.measuredWidth();

    3.计算子视图的高度和宽度。例如,两个孩子各占父视图高度的 50%,但占父视图的全宽:

    int childHeight = height/2;
    int childWidth = width;
    

    4.根据父类型获取instanceof LayoutParams。

    GridLayout.LayoutParams params = new GridLayout.LayoutParams();
    

    5。在布局参数上设置高度和宽度。

     params.width = childWidth;
     params.height = childHeight;
    

    6.创建子视图并设置布局参数。

    ImageView imageview = new ImageView();
    imageview.setLayoutParams(params);
    

    7.将子视图添加到父视图。

     gridLayout.addView(ImageView);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-06-24
      • 1970-01-01
      • 2013-08-09
      • 2017-05-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多