2012-02-09 18 views
6

tu jesteś mój problem: Chcę zrobić układ, który wygląda następująco sieci w Androidzie:TableLayout Android i przycisk zmiany rozmiaru

enter image description here

W tym celu, stworzyłem ten układ:

 <?xml version="1.0" encoding="utf-8"?> 
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/RelativeLayout1" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:gravity="center" 
    android:orientation="vertical" > 


    <TableLayout 
    android:id="@+id/tableLayout1" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" > 

    <TableRow 
     android:id="@+id/tableRow1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_weight="0.3"> 

     <Button 
      android:id="@+id/clientimain" 
      android:layout_width="0dip" 
      android:layout_weight="1" android:layout_height="fill_parent"/> 



     <Button 
      android:id="@+id/button1" 
      android:layout_width="0dip" 
      android:layout_height="fill_parent" 
      android:layout_weight="1" 
      android:text="Button" /> 

    </TableRow> 

    <TableRow 
     android:id="@+id/tableRow2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_weight="0.3" > 

     <Button 
      android:id="@+id/button2" 
      android:layout_width="0dip" 
      android:layout_height="fill_parent" 
      android:layout_weight="1" 
      android:text="Button"/> 

     <Button 
      android:id="@+id/button6" 
      android:layout_width="0dip" 
      android:layout_height="fill_parent" 
      android:layout_weight="1" 
      android:text="Button" /> 

    </TableRow> 

    <TableRow 
     android:id="@+id/tableRow3" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_weight="0.3" > 

     <Button 
      android:id="@+id/button3" 
      android:layout_width="0dip" 
      android:layout_height="fill_parent" 
      android:layout_weight="1" 
      android:text="Button" 
      /> 

     <Button 
      android:id="@+id/button7" 
      android:layout_width="0dip" 
      android:layout_height="fill_parent" 
      android:layout_weight="1" 
      android:text="Button" 
      /> 

    </TableRow> 

    </TableLayout> 

    </RelativeLayout> 

W zasadzie składa się z 6 przycisków w układzie tabeli z prawidłowo ustawioną właściwością masy (lub mam taką nadzieję!). Wynik wygląda dobrze, dopóki nie spróbuję ustawić właściwości tła każdego przycisku. W takim przypadku przycisk przyjmuje wysokość obrazu, który wstawiono (szerokość jest w porządku). Na przykład jeżeli ustawić obraz tła których rozmiar jest x 96 pikseli x x 96 pikseli wynik wyglądać następująco:

enter image description here

Czy istnieje sposób, aby zapobiec tego przycisk „rozciągnięcie” i poprawnie wycentrować obraz? Próbowałem zmienić właściwość height dla każdego wiersza tabeli, aby zmienić właściwość max height button, a także zmienić typ przycisku za pomocą przycisku obrazu (i ustawić właściwość src z ikoną wanted), ale nie osiągnąłem pożądanego wynik.
Mam również czerwoną dokumentację google dotyczącą obsługi wielu ekranów i każdego z podstawowych układów, ale nie znalazłem żadnego rozwiązania. Z góry dziękuję wszystkim, którzy chcieliby mi pomóc!

Andrea

+0

spróbuj i użyj Androida: scaleType = "fitXY" na swoim przycisku. spróbuj też użyć http://developer.android.com/resources/tutorials/views/hello-gridview.html dla tego zadania. –

+0

Próbowałem atrybutu android: scaleType = "fitXY", ale nie działało. Spróbuję przejść do układu siatki, jeśli nie mogę znaleźć rozwiązania. Dzięki – jiraya85

Odpowiedz

3

Ustaw wysokość układu wiersza tabeli jako 0dip i ustawić wagę jako 1

<TableRow 
      android:id="@+id/tableRow1" 
      android:layout_width="wrap_content" 
      android:layout_height="0dp" 
      android:layout_weight="1" > 

To powinno rozwiązać problem


Zmień

<Button 
      android:id="@+id/clientimain" 
      android:layout_width="0dip" 
      android:layout_weight="1" android:layout_height="fill_parent"/> 

to

<Button 
      android:id="@+id/clientimain" 
      android:layout_width="0dip" 
      android:layout_weight="1" android:layout_height="fill_parent"/> 

to może działać, warto spróbować, w Eclipse dla mnie to czyni prawo jak trzeba było to pierwsze, to może spróbować czyszczenia projektu (Project> czysty projekt), ponieważ powinno być prawo, ponieważ jest


kod powinien wyglądać następująco:

<?xml version="1.0" encoding="utf-8"?> 
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/RelativeLayout1" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:gravity="center" 
    android:orientation="vertical" > 


    <TableLayout 
     android:id="@+id/tableLayout1" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" > 

     <TableRow 
      android:id="@+id/tableRow1" 
      android:layout_width="wrap_content" 
      android:layout_height="0dp" 
      android:layout_weight="1" > 

      <Button 
       android:id="@+id/clientimain" 
       android:layout_width="0dip" 
       android:layout_height="fill_parent" 
       android:layout_weight="1" /> 

      <Button 
       android:id="@+id/button1" 
       android:layout_width="0dip" 
       android:layout_height="fill_parent" 
       android:layout_weight="1" 
       android:text="Button" /> 
     </TableRow> 

     <TableRow 
      android:id="@+id/tableRow2" 
      android:layout_width="wrap_content" 
      android:layout_height="0dp" 
      android:layout_weight="1" > 

      <Button 
       android:id="@+id/button2" 
       android:layout_width="0dip" 
       android:layout_height="fill_parent" 
       android:layout_weight="1" 
       android:text="Button" /> 

      <Button 
       android:id="@+id/button6" 
       android:layout_width="0dip" 
       android:layout_height="fill_parent" 
       android:layout_weight="1" 
       android:text="Button" /> 
     </TableRow> 

     <TableRow 
      android:id="@+id/tableRow3" 
      android:layout_width="wrap_content" 
      android:layout_height="0dp" 
      android:layout_weight="1" > 

      <Button 
       android:id="@+id/button3" 
       android:layout_width="0dip" 
       android:layout_height="fill_parent" 
       android:layout_weight="1" 
       android:text="Button" /> 

      <Button 
       android:id="@+id/button7" 
       android:layout_width="0dip" 
       android:layout_height="fill_parent" 
       android:layout_weight="1" 
       android:text="Button" /> 
     </TableRow> 
    </TableLayout> 

    </RelativeLayout> 

<?xml version="1.0" encoding="utf-8"?> 
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/RelativeLayout1" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:gravity="center" 
    android:orientation="vertical" > 


    <TableLayout 
     android:id="@+id/tableLayout1" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" > 


     <TableRow 
      android:id="@+id/tableRow1" 
      android:layout_width="wrap_content" 
      android:layout_height="0dp" 
      android:layout_weight="1" > 

      <ImageButton 
       android:id="@+id/imageButton1" 
       android:layout_width="0dp" 
       android:layout_height="fill_parent" 
       android:layout_weight="1" 
       android:src="@drawable/ic_launcher" /> 

      <ImageButton 
       android:id="@+id/imageButton2" 
       android:layout_width="0dp" 
       android:layout_height="fill_parent" 
       android:layout_weight="1" 
       android:src="@drawable/ic_launcher" /> 
     </TableRow> 


     <TableRow 
      android:id="@+id/tableRow2" 
      android:layout_width="wrap_content" 
      android:layout_height="0dp" 
      android:layout_weight="1" > 

      <ImageButton 
       android:id="@+id/imageButton3" 
       android:layout_width="0dp" 
       android:layout_height="match_parent" 
       android:layout_weight="1" 
       android:src="@drawable/ic_launcher" /> 

      <ImageButton 
       android:id="@+id/imageButton4" 
       android:layout_width="0dp" 
       android:layout_height="match_parent" 
       android:layout_weight="1" 
       android:src="@drawable/ic_launcher" /> 
     </TableRow> 


     <TableRow 
      android:id="@+id/tableRow3" 
      android:layout_width="wrap_content" 
      android:layout_height="0dp" 
      android:layout_weight="1" > 

      <ImageButton 
       android:id="@+id/imageButton5" 
       android:layout_width="0dp" 
       android:layout_height="match_parent" 
       android:layout_weight="1" 
       android:src="@drawable/ic_launcher" /> 

      <ImageButton 
       android:id="@+id/imageButton6" 
       android:layout_width="0dp" 
       android:layout_height="match_parent" 
       android:layout_weight="1" 
       android:src="@drawable/ic_launcher" /> 
     </TableRow> 

    </TableLayout> 

    </RelativeLayout> 

wciąż nic nie zmienia, jak duży jest rozmiar obrazu?

+0

Próbowałem już to zrobić, ale bez rezultatu. Teraz każdy rząd tabeli ma te właściwości (wysokość 0 i waga 1), ale wciąż mam ten odcinek. Dzięki – jiraya85

+0

Przede wszystkim dziękuję za wsparcie. Próbowałem oczyścić projekt, a także zamienić kod na Twój, ale gdy wybieram obraz tła, nadal mam ten sam problem. Jeśli nie zastosuję żadnego obrazu na tle przycisku, wszystko jest w porządku, a siatka wygląda tak, jakbym chciała! – jiraya85

+0

Hmm w porządku, chory, spróbuj z obrazami, po prostu poczekaj kilka, wrócę – FabianCook

0

Kiedy zastosowano obraz tła jako

android:background="@drawable/ic_launcher" 

to działało dobrze.

Myślałem, że to ze względu na rozmiar obrazu, więc zastosowałem inny obraz o rozmiarze 160X160 i problem powrócił. Niż zastosowałem ten sam obraz 160X160 do wszystkich przycisków i wyrównały się idealnie. tutaj jest mój kod (Obraz Gender.png ma 160x160 pikseli)

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/RelativeLayout1" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:gravity="center" 
android:orientation="vertical" > 

<TableLayout 
android:id="@+id/tableLayout1" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" > 

<TableRow 
    android:id="@+id/tableRow1" 
    android:layout_width="wrap_content" 
    android:layout_height="fill_parent" 
    android:layout_weight="0.3"> 

    <Button 
     android:id="@+id/clientimain" 
     android:layout_width="0dip" 
     android:layout_height="fill_parent" 
     android:layout_weight="1" 
     android:background="@drawable/Gender" /> 

    <Button 
     android:id="@+id/button1" 
     android:layout_width="0dip" 
     android:layout_height="fill_parent" 
     android:layout_weight="1" 
     android:text="Button" 
     android:background="@drawable/Gender"/> 

</TableRow> 

<TableRow 
    android:id="@+id/tableRow2" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_weight="0.3" > 

    <Button 
     android:id="@+id/button2" 
     android:layout_width="0dip" 
     android:layout_height="fill_parent" 
     android:layout_weight="1" 
     android:text="Button" 
     android:background="@drawable/Gender"/> 

    <Button 
     android:id="@+id/button6" 
     android:layout_width="0dip" 
     android:layout_height="fill_parent" 
     android:layout_weight="1" 
     android:text="Button" 
     android:background="@drawable/Gender"/> 

</TableRow> 

<TableRow 
    android:id="@+id/tableRow3" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_weight="0.3" 
    > 

    <Button 
     android:id="@+id/button3" 
     android:layout_width="0dip" 
     android:layout_height="fill_parent" 
     android:layout_weight="1" 
     android:text="Button" 
     android:background="@drawable/Gender" /> 

    <Button 
     android:id="@+id/button7" 
     android:layout_width="0dip" 
     android:layout_height="fill_parent" 
     android:layout_weight="1" 
     android:text="Button" 
     android:background="@drawable/Gender"/> 

</TableRow> 


    <TableRow 
    android:id="@+id/tableRow4" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_weight="0.3" > 

    <Button 
     android:id="@+id/button77" 
     android:layout_width="0dip" 
     android:layout_height="fill_parent" 
     android:layout_weight="1" 
     android:text="Button" 
     android:background="@drawable/Gender" 
     /> 

    <Button 
     android:id="@+id/button75" 
     android:layout_width="0dip" 
     android:layout_height="fill_parent" 
     android:layout_weight="1" 
     android:text="Button" 
     android:background="@drawable/Gender" 
     /> 

</TableRow> 


</TableLayout> 

1

wiem, to nie jest dokładnie to, czego chciał, ale zalecamy użycie GridView zamiast układu TableRow.

Myślę, że GridView powinien działać lepiej, ponieważ jest zaimplementowany z widokiem recyklingu i rzeczy odziedziczonych po AbsListView. GridView jest trudniejszy do wdrożenia, ponieważ musisz używać go z Adapterem, ale będzie działać wydajnie, jeśli masz dużo ciężkich widoków do załadowania, takich jak obrazy. Reference.

<GridView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/gridView1" 
    android:numColumns="auto_fit" 
    android:stretchMode="columnWidth" 
    android:columnWidth="100dp" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" > 

</GridView> 
Powiązane problemy