2017-07-05 71 views
0

我有一个简单的线性布局,并希望将屏幕分成两个大小相等的部分。适合父级布局的大小

在上面的那个,我想要放一个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> 

回答

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

更新:回答以上是关于重用法正确,确保无论是HIGHT或宽度是在您使用重量所有地方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> 
+0

不适用于我。那么'MySurfaceView'不可见。 – user1225905

+0

@ user1225905查看已更新的答案 – Sharj

+0

好的,没有第一个嵌套内部'LinearLayout'的方法可行,但我不确定是否有LinearLayout背后的逻辑。当我在'MySurvfaceView'周围添加'weight = 1'和'height = 0'的'LinearLayout'时,它不起作用。 – user1225905

0

编程:

  1. 寻父布局。

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

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

    int height = gridLayout.measuredHeight();

    int width = gridLayout.measuredWidth();

3.Calculate子视图的高度和width.For例如两个子每次取在父高度的50%但考虑父的全宽度:

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

4.Get的instanceof取决于的LayoutParams父类型。

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

5.设置布局参数的高度和宽度。

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

6.Create子视图,设置布局PARAMS。

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

7.添加子查看父项。

gridLayout.addView(ImageView);