2014-01-21 42 views
1

我很努力与android布局设置。无论内容是什么,我都想保持相同的固定大小,并且想要在中心显示所有内容!Android固定布局3/4 + 1/4

--------------------------------- 
-      -  - 
-      -  - 
-  3/4 (center)  - 1/4 - 
-      - (ct) - 
-      -  - 
-      -  - 
--------------------------------- 

这里是我的layout.xml代码,但它不工作:

<LinearLayout 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignParentTop="true" 
      android:layout_centerHorizontal="true" 
      android:layout_centerInParent="true" 
      android:orientation="horizontal" 
      android:weightSum="1" > 

     <VideoView 
      android:id="@+id/videoView1" 
      android:layout_height="wrap_content" 
      android:layout_width="fill_parent" 
      android:layout_gravity="center" 
      android:layout_weight=".25" /> 

     <TextView 
      android:id="@+id/textView1" 
      android:layout_height="wrap_content" 
      android:layout_width="fill_parent" 
      android:layout_gravity="center" 
      android:layout_weight=".75" 
      android:freezesText="false" 
      android:includeFontPadding="false" 
      android:scrollHorizontally="false" 
      android:selectAllOnFocus="false" 
      android:text="@string/Runstring" /> 
     </LinearLayout> 

编辑:

我试图从下面和经历了一些代码:如果我做@string/Runstring =只是一个“一个”,或者如果我做到“..........长时间......”它正在改变整个布局!这不是保持3/4 + 1/4的比例!这是为什么?

回答

1

与布局的问题是,您已经定义父布局的宽度为WRAP_CONTENT则必须将此作为FILL_PARENT。您将视图分成一定比例,但您尚未将该属性设置为fill_parent。这就是为什么它不能将屏幕分成正确的比例。

这是你更新的布局

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" > 

    <LinearLayout 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal" 
     android:weightSum="1" > 

     <VideoView 
      android:id="@+id/videoView1" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center" 
      android:layout_weight=".25" /> 

     <TextView 
      android:id="@+id/textView1" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center" 
      android:layout_weight=".75" 
      android:freezesText="false" 
      android:includeFontPadding="false" 
      android:scrollHorizontally="false" 
      android:selectAllOnFocus="false" 
      android:text="@string/Runstring" /> 
    </LinearLayout> 

</LinearLayout> 
+0

谢谢!似乎工作! – Raymond

2

变化

android:layout_width="fill_parent" 

android:layout_width="0dp" 

layout_weight将宽度设置为0dp工作。您的布局将类似于:

<LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignParentTop="true" 
     android:layout_centerHorizontal="true" 
     android:layout_centerInParent="true" 
     android:orientation="horizontal" 
     android:weightSum="4" > 

    <VideoView 
     android:id="@+id/videoView1" 
     android:layout_height="wrap_content" 
     android:layout_width="0dp" 
     android:layout_gravity="center" 
     android:layout_weight="3" /> 

    <TextView 
     android:id="@+id/textView1" 
     android:layout_height="wrap_content" 
     android:layout_width="0dp" 
     android:layout_gravity="center" 
     android:layout_weight="1" 
     android:freezesText="false" 
     android:includeFontPadding="false" 
     android:scrollHorizontally="false" 
     android:selectAllOnFocus="false" 
     android:text="@string/Runstring" /> 
    </LinearLayout> 
+2

“layout_weight只有作品宽度设置为0dp“。那是不正确的。 –

+0

以我的经验看到,将它设置为match_parent或wrap_content会产生不良效果。将很高兴知道其他值与重量有什么关系:-) –

+0

检查更新后的答案。这是真正的问题。 :) –

0

在代码中,你可以做这样的

metrics = getResources().getDisplayMetrics(); 
     height = metrics.heightPixels; 
     weight = metrics.weightPixels;//RETURNS THE SCREEN WIDTH 
    videosView1.setLayoutParams(new LinearLayout.LayoutParams(
       width/4,LinearLayout.LayoutParams.MATCH_PARENT)); 
    TEXTVIEW1.setLayoutParams(new LinearLayout.LayoutParams(
       3*width/4,LinearLayout.LayoutParams.MATCH_PARENT)); 

或在XML中你可以像本

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" > 

    <LinearLayout 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal" > 

     <VideoView 
      android:id="@+id/videoView1" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center" 
      android:layout_weight="3" /> 

     <TextView 
      android:id="@+id/textView1" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center" 
      android:layout_weight="1" 
      android:freezesText="false" 
      android:includeFontPadding="false" 
      android:scrollHorizontally="false" 
      android:selectAllOnFocus="false" 
      android:text="@string/Runstring" /> 
    </LinearLayout> 

</LinearLayout>