2014-01-21 5 views
1

Я борюсь с настройками макета Android. Я хотел бы сохранить один и тот же фиксированный размер независимо от того, что такое контент, и хочу показать все в центре!Android fixed layout 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> 

EDIT:

Я попробовал некоторые коды из ниже и опытных: Если я делаю @string/Runstring = просто «а», или если я это сделаю ».......... LONG STRING .........« IT меняет всю компоновку! Он не поддерживает соотношение 3/4 + 1/4! Почему это?

ответ

1

Проблема макет в том, что вы определили ширину макета Родителя, как wrap_content вы должны установить это как fill_parent. Вы разделяете представления на некоторую долю, но вы не устанавливаете атрибут 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> 
Смежные вопросы