2014-01-21 43 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>