2012-06-28 10 views
6

me encontré con este enlace:ajustar el ancho de la pantalla a la mitad

Assign width to half available screen width declaratively

Cómo hacer lo mismo en la disposición relativa? Aquí está el código: Probar el diseño lineal dentro del diseño relativo. Pero esto no funciona.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:background="@drawable/bg" > 

    <LinearLayout 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:orientation="horizontal" > 

     <Button 
      android:id="@+id/button5" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:layout_marginTop="14dp" 
      android:layout_weight="1" 
      android:text="Button" /> 

     <Button 
      android:id="@+id/button6" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:layout_marginTop="16dp" 
      android:layout_weight="1" 
      android:text="Button" /> 

     <Button 
      android:id="@+id/button4" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:layout_alignParentLeft="true" 
      android:layout_alignParentTop="true" 
      android:layout_weight="1" 
      android:text="UID" 
      android:textColor="#ffe5f1f1" /> 

     <EditText 
      android:id="@+id/editText1" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:background="#ffe5f1f1" 
      android:hint="Enter UID" 
      android:inputType="number" 
      android:shadowColor="5" 
      android:textColor="#ff009999" 
      android:textStyle="bold" 
      android:typeface="monospace" > 

      <requestFocus /> 
     </EditText> 



    <Button 
     android:id="@+id/button7" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
      android:layout_weight="1" 
     android:layout_alignParentLeft="true" 
     android:layout_marginTop="18dp" 
     android:text="Button" /> 

    <Button 
     android:id="@+id/button8" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_below="@+id/button7" 
      android:layout_weight="1" 
     android:layout_marginTop="16dp" 
     android:text="Button" /> 

    <Button 
     android:id="@+id/button9" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
      android:layout_weight="1" 
     android:layout_alignParentLeft="true" 
     android:layout_below="@+id/button8" 
     android:layout_marginTop="17dp" 
     android:text="Button" /> 

    <EditText 
     android:id="@+id/editText2" 
     android:layout_width="50" 
     android:layout_height="wrap_content" 
     android:layout_alignParentRight="true" 
     android:layout_weight="1" 
     android:background="#ffe5f1f1" 
     android:ems="10" 
     android:hint="Enter Name" 
     android:inputType="number" 
     android:inputType="textPersonName" 
     android:textColor="#ff009999" 
     android:textStyle="bold" 
      android:layout_weight="1" 
     android:typeface="monospace" /> 

    <EditText 
     android:id="@+id/editText3" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignParentRight="true" 
     android:ems="10" 
      android:layout_weight="1" 
     android:inputType="number" /> 

    <EditText 
     android:id="@+id/editText4" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignBaseline="@+id/button7" 
     android:layout_alignBottom="@+id/button7" 
     android:layout_alignParentRight="true" 
     android:ems="10" 
      android:layout_weight="1" 
     android:inputType="phone" /> 

    <EditText 
     android:id="@+id/editText5" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignBaseline="@+id/button8" 
     android:layout_alignBottom="@+id/button8" 
     android:layout_alignParentRight="true" 
     android:ems="10" 
      android:layout_weight="1" 
     android:inputType="textPersonName" /> 

    <RadioButton 
     android:id="@+id/radioButton2" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignBaseline="@+id/button9" 
     android:layout_alignBottom="@+id/button9" 
     android:layout_alignLeft="@+id/editText5" 
      android:layout_weight="1" 
     android:text="Male" /> 

    <RadioButton 
     android:id="@+id/radioButton1" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignBaseline="@+id/radioButton2" 
     android:layout_alignBottom="@+id/radioButton2" 
     android:layout_alignParentRight="true" 
      android:layout_weight="1" 
     android:text="Female" /> 

    <Button 
     android:id="@+id/button10" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_below="@+id/button9" 
     android:layout_marginTop="31dp" 
      android:layout_weight="1" 
     android:text="Button" /> 

    <Spinner 
     android:id="@+id/spinDept" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignBaseline="@+id/button10" 
     android:layout_alignBottom="@+id/button10" 
     android:layout_alignLeft="@+id/radioButton2" 
     android:layout_alignParentRight="true" 
     android:layout_weight="1" 
     android:background="#ff009999" /> 
    </LinearLayout> 
</RelativeLayout> 
+0

lo que realmente quiere sumergido en diseño de la pantalla o de los widgets – MobileEvangelist

+0

hacen un formulario tipo de diseño que es compatible con todos los tamaños de pantalla. – dreamhigh

+0

no entiendo tu pregunta. la pregunta vinculada es sobre dividir una vista en 2 mitades. tienes una docena de vistas en tu diseño lineal. ¿Qué estás tratando de hacer? – njzk2

Respuesta

4

Envuelva la misma cosa con la disposición relativa que debería funcionar para usted.

<LinearLayout android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal"> 
    <Button android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:text="somebutton"> 

    <TextView android:layout_width="fill_parent" 
      android:layout_height="Wrap_content" 
      android:layout_weight="1"> 
</LinearLayout> 
+0

¿Entonces tendré que usar el diseño lineal varias veces? Tengo casi más de 5 filas similares en un diseño relativo. – dreamhigh

+0

Tiene un diseño relativo dentro del cual puede tener un diseño lineal que contiene toda su fila, deje que sea uno, dos o 100. –

+0

no funciona :( – dreamhigh

1
Display display = getWindowManager().getDefaultDisplay(); 
    int width = display.getWidth(); 
    int height = display.getHeight(); 
    plannerEditLayout = (RelativeLayout) findViewById(R.id.plannerLayoutEdit); 
    plannerEditLayout.setGravity(Gravity.CENTER_VERTICAL); 
    plannerEditLayout.setLayoutParams(new RelativeLayout.LayoutParams(
      (width/2), height)); 

Puedo utilizar el mismo código para LinearLayout. Havent lo probó para RelativeLayout. Dale un tiro. Comenta si no funciona.

-1
<LinearLayout android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal"> 
    <Button android:layout_width="match_parent" 
      android:id="@+id/button" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:text="somebutton"> 

    <TextView android:layout_width="match_parent" 
      android:id="@+id/textview" 
      android:layout_height="Wrap_content" 
      android:layout_weight="1"> 
</LinearLayout> 

En ejemplo anterior utilizado LinearLayout, conjunto de widgets layout_width a "match_parent" y proporcionar peso (para especificar área a ocupar).

Esto resuelve su problema.

+0

No. Cada elemento tiene identificación definida. No importa con eso. – dreamhigh

+0

android: layout_weight = "1" no funciona en RelativeLayout –

51

Esta es una buena solución si necesita exactamente la mitad, cuando usa RelativeLayout. Lo que se hace es, hacer una vista invisible falsa centrada en los padres, y han dejado botón sea alignParentLeft y bside a ese punto de vista falso y el botón derecho viceversa

<RelativeLayout 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content"> 

    <View android:id="@+id/fakeView" 
     android:layout_width="0dp" 
     android:layout_height="0dp" 
     android:layout_centerInParent="true"/> 
    <Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignRight="@id/fakeView" 
     android:layout_alignParentLeft="true" 
     android:text="Left"/> 

    <Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignLeft="@id/fakeView" 
     android:layout_alignParentRight="true" 
     android:text="Right"/> 

</RelativeLayout> 

Su realmente inteligente.

+0

Estoy de acuerdo, muy inteligente. Me encantaría saber si esto es realmente más rápido en la prestación que utilizando un LinearLayout. – zarazan

+0

Idk, probablemente no, pero en una vista más general, pero si Si está usando el diseño relativo para empezar, esto sería más rápido porque elimina diseños anidados – urSus

2

Además de respuesta Ursus 's, es mejor utilizar una clase en lugar de ViewSpace:

<Space android:id="@+id/fakeView" 
     android:layout_width="0dp" 
     android:layout_height="0dp" 
     android:layout_centerInParent="true"/> 
+1

No estoy de acuerdo. El espacio solo está disponible para api14 +, lo que lo hace virtualmente inútil si quiere apuntar a una amplia gama de dispositivos. – njzk2

+0

bien, api14 + es 4.0 + y según la última tabla de distribución de plataforma (http://goo.gl/fmFDJw), la cantidad de dispositivos con h api <14 ahora es aproximadamente 15%, que ya es casi insignificante – Dmide

+2

si encuentra 15% es insignificante, bueno para usted. también esta tabla solo mide dispositivos con acceso a Google Play Store, que está lejos de ser todos los dispositivos. – njzk2

Cuestiones relacionadas