2012-06-28 21 views
6

natknąłem się na ten link:Regulacja szerokości do połowy ekranu

Assign width to half available screen width declaratively

Jak zrobić to samo w układzie względnym? Oto kod: Próbuję linelayout wewnątrz względnego układu. Ale to nie działa.

<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

co rzeczywiście chcesz zanurkował układ ekranu lub jego widżety – MobileEvangelist

+0

dokonać pewnego rodzaju formą układ który jest zgodny z wszystkimi rozmiarami ekranu. – dreamhigh

+0

Nie rozumiem twojego pytania. powiązane pytanie dotyczy podziału widoku na dwie połówki. masz kilkanaście widoków w układzie liniowym. co próbujesz zrobić? – njzk2

Odpowiedz

4

Zawiń to samo z układem względnym, który powinien zadziałać.

<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

Więc będę musiał użyć liniowego układu kilka razy? Mam prawie więcej niż 5 wierszy podobnych do niego we względnym układzie. – dreamhigh

+0

Masz jeden układ względny, wewnątrz którego możesz mieć Układ liniowy, który zawiera cały wiersz, niech będzie jeden, dwa lub 100. –

+0

nie działa :( – 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)); 

mogę użyć tego samego kodu dla LinearLayout. Nie próbowałem go na myslef dla RelativeLayout. Spróbuj. Komentarz, jeśli nie działa.

-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> 

W powyższym przykładzie stosuje LinearLayout ustawić widżet layout_width do „match_parent” i zapewnia ciężar (w celu określenia obszaru do zajmowania).

To rozwiązuje twój problem.

+0

Nie. Każdy element ma zdefiniowany identyfikator Nie ma to znaczenia. – dreamhigh

+0

android: layout_weight = "1" nie działa w RelativeLayout –

51

Jest to miłe obejście, jeśli potrzebujesz dokładnie połowy, używając RelativeLayout. Co zrobić, to, zrobić fałszywy pogląd skupione w niewidzialną rodzica, i lewy przycisk być alignParentLeft i alignRight do tego fałszywego widzenia i prawy przycisk odwrotnie

<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> 

Jej naprawdę mądry.

+0

Zgadzam się, bardzo sprytnie. Chciałbym wiedzieć, czy to jest rzeczywiście szybsze przy renderowaniu niż przy użyciu LinearLayout. – zarazan

+0

Idk, prawdopodobnie nie, ale w bardziej ogólnym widoku, ale jeśli na początku korzystasz z układu względnego, byłoby to szybsze, ponieważ wyeliminowałeś układy zagnieżdżone – urSus

2

Oprócz odpowiedzi Ursus „s, to lepiej użyć Space klasę zamiast View:

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

Nie zgadzam się.Spacja jest dostępna tylko dla api14 +, co czyni ją praktycznie bezużyteczną, jeśli chcesz kierować na szeroką gamę urządzeń – njzk2

+0

cóż, api14 + to 4.0 + i zgodnie z najnowszą tabelą dystrybucji platform (http://goo.gl/fmFDJw), ilość urządzeń z dowcipem h api <14 wynosi teraz około 15%, co jest już prawie nieistotne. – Dmide

+2

jeśli okaże się, że 15% jest nieistotne, dobre dla ciebie. także ten wykres mierzy tylko urządzenia z dostępem do sklepu Google Play, który jest daleki od bycia wszystkimi urządzeniami. – njzk2

Powiązane problemy