2013-03-04 21 views
6

Ho bisogno di impostare la larghezza della mia vista come 50% della larghezza dello schermo e quindi centrare questa vista orizzontalmente mentre potenzialmente ho uno o più pulsanti che possono apparire attaccati al lato sinistro o destro di lo schermo.Layout larghezza percentuale Android

Sto utilizzando un layout relativo in modo che sia possibile posizionare un layout lineare con pesi per ottenere il 50% centrato mentre si posiziona qualsiasi pulsante sopra a LL collegato al bordo sinistro o destro di RL. Tuttavia questo layout manca la barra centrale blu. Se imposto la vista centrale layout_weight su 1 ottengo 3 barre di uguale dimensione.

<RelativeLayout 
    android:layout_width="match_parent" 
    android:layout_height="48dp"> 

    <LinearLayout 
     android:id="@+id/stupid_android" 
     android:orientation="horizontal" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" > 

     <View 
      android:layout_width="fill_parent" 
      android:layout_height="match_parent" 
      android:background="#FF0000" 
      android:layout_weight="1" /> 

     <View 
      android:layout_width="fill_parent" 
      android:layout_height="match_parent" 
      android:background="#0000FF" 
      android:layout_weight="2" /> 

     <View 
      android:layout_width="fill_parent" 
      android:layout_height="match_parent" 
      android:background="#00FF00" 
      android:layout_weight="1" /> 

    </LinearLayout> 

</RelativeLayout> 

risposta

15

È necessario impostare la larghezza di vista 0dip

<RelativeLayout 
    android:layout_width="match_parent" 
    android:layout_height="48dp"> 

    <LinearLayout 
     android:id="@+id/stupid_android" 
     android:orientation="horizontal" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" > 

     <View 
      android:layout_width="0dip" 
      android:layout_height="match_parent" 
      android:background="#FF0000" 
      android:layout_weight="1" /> 

     <View 
      android:layout_width="0dip" 
      android:layout_height="match_parent" 
      android:background="#0000FF" 
      android:layout_weight="2" /> 

     <View 
      android:layout_width="0dip" 
      android:layout_height="match_parent" 
      android:background="#00FF00" 
      android:layout_weight="1" /> 

    </LinearLayout> 

</RelativeLayout> 
+1

Ecco come funziona 'layout_weight'. 'layout_weight' dice come ** lo ** spazio rimanente nella parent' View' dovrebbe essere diviso dai 'View' dei bambini. Nel tuo caso, per prima cosa 'View' ha' android: layout_width' impostato su 'fill_parent' quindi non c'è effettivamente spazio rimanente. Non so davvero perché l'ultima vista sia ancora visibile, perché ho deciso di vedere solo la prima. –

-2

Se ho capito bene, avete bisogno di questo:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="48dp"> 

    <LinearLayout 
     android:id="@+id/stupid_android" 
     android:orientation="horizontal" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"> 

     <View 
      android:layout_width="fill_parent" 
      android:layout_height="match_parent" 
      android:background="#FF0000" 
      android:layout_weight="1" ></View> 

     <View 
      android:layout_width="fill_parent" 
      android:layout_height="match_parent" 
      android:background="#0000FF" 
      android:layout_weight="1.5" ></View> 

     <View 
      android:layout_width="fill_parent" 
      android:layout_height="match_parent" 
      android:background="#00FF00" 
      android:layout_weight="1.5" ></View> 

    </LinearLayout> 

</RelativeLayout> 
1

È possibile raggiungere questo obiettivo con la nuova libreria per cento:

https://developer.android.com/tools/support-library/features.html#percent

facendo qualcosa di simile a questo:

<android.support.percent.PercentRelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:layout_width="match_parent" 
    android:layout_height="48dp"> 

    <View 
     android:layout_width="0dp" 
     android:layout_height="match_parent" 
     android:background="#FF0000" 
     app:layout_widthPercent="25%" /> 

    <View 
     android:layout_width="0dp" 
     android:layout_height="match_parent" 
     android:layout_weight="2" 
     android:background="#0000FF" 
     android:centerHorizontal="true" 
     app:layout_marginLeftPercent="25%" 
     app:layout_widthPercent="50%" /> 

    <View 
     android:layout_width="0dp" 
     android:layout_height="match_parent" 
     android:alignParentRight="true" 
     android:background="#00FF00" 
     app:layout_marginLeftPercent="75%" 
     app:layout_widthPercent="25%" /> 

</android.support.percent.PercentRelativeLayout> 
0

Usa nuova libreria di supporto percentuale.

compile 'com.android.support:percent:24.0.0' 

Esempio:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:id="@+id/main" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 

    <android.support.percent.PercentRelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="48dp" 

     > 

     <View 
      android:layout_height="match_parent" 
      android:layout_weight="1" 
      android:background="#FF0000" 
      app:layout_widthPercent="33%" /> 

     <View 
      android:layout_height="match_parent" 
      android:layout_weight="2" 
      android:background="#0000FF" 
      app:layout_marginLeftPercent="33%" 
      app:layout_widthPercent="33%" /> 

     <View 
      android:layout_height="match_parent" 
      android:layout_weight="1" 
      android:background="#00FF00" 
      app:layout_marginLeftPercent="66%" 
      app:layout_widthPercent="33%" /> 

    </android.support.percent.PercentRelativeLayout> 


</LinearLayout> 

Per ulteriori informazioni Android Doc

* per il campione & Tutorial * Tutorial1Tutorial2Tutorial3

Problemi correlati