2012-02-09 35 views
6

đây bạn là vấn đề của tôi: Tôi muốn làm một bố cục trông giống như lưới này trong android:TableLayout và nút Android thay đổi kích thước

enter image description here

Để làm như vậy, tôi đã tạo bố trí này:

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

Vì vậy, về cơ bản nó được tạo thành bởi 6 nút trong bố cục bảng với thuộc tính trọng số được đặt chính xác (hoặc tôi hy vọng như vậy!). Kết quả có vẻ tốt cho đến khi tôi sẽ cố gắng thiết lập thuộc tính nền của mỗi nút. Trong trường hợp đó, nút sẽ lấy chiều cao của hình ảnh được đặt vào (chiều rộng là ok). Ví dụ nếu tôi đặt hình nền có kích thước là x 96px x x 96px kết quả giống như thế này:

enter image description here

Có cách nào để ngăn chặn nút này "căng" và để tập trung một cách chính xác hình ảnh? Tôi đã cố thay đổi thuộc tính chiều cao cho mỗi hàng trong bảng, để thay đổi thuộc tính chiều cao tối đa của nút và cũng có thể thay đổi loại nút bằng nút hình ảnh (và đặt thuộc tính src với biểu tượng mong muốn) nhưng tôi không hoàn thành kết quả.
Tôi cũng đã làm tài liệu google về hỗ trợ nhiều màn hình và về từng bố cục cơ bản nhưng tôi không tìm thấy bất kỳ giải pháp nào. Cảm ơn trước bất kỳ ai muốn giúp tôi!

Andrea

+0

thử và sử dụng android: scaleType = "fitXY" trên nút của bạn. cũng thử và sử dụng http://developer.android.com/resources/tutorials/views/hello-gridview.html cho tác vụ này –

+0

Tôi đã thử thuộc tính android: scaleType = "fitXY" nhưng nó không hoạt động. Tôi sẽ cố gắng chuyển sang bố cục lưới nếu tôi không thể tìm thấy giải pháp. Cảm ơn – jiraya85

Trả lời

3

Đặt chiều cao bố trí của các dòng của bảng như 0dip và thiết lập cân như 1

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

này cần khắc phục vấn đề của bạn


Thay đổi

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

tới

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

này có thể làm việc, đáng thử, trong nhật thực đối với tôi nó làm cho đúng cách bạn đã có nó đầu tiên, có lẽ bạn có thể thử làm sạch dự án của bạn (Dự án> Dự án sạch) vì nó phải đúng vì nó là


Bạn mã nên được như thế này:

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

vẫn không thay đổi bất kỳ thứ gì, kích thước của hình ảnh lớn đến cỡ nào?

+0

Tôi đã cố gắng để làm điều đó nhưng không có kết quả. Bây giờ mỗi hàng của bảng đã có các thuộc tính đó (chiều cao 0 và trọng số 1) nhưng tôi vẫn có độ dài đó. Cảm ơn – jiraya85

+0

Trước hết, cảm ơn sự hỗ trợ của bạn. Tôi đã cố gắng để làm sạch dự án và cũng để thay thế mã của tôi với bạn nhưng khi tôi chọn một hình nền, tôi vẫn có cùng một vấn đề. Nếu tôi không áp dụng bất kỳ hình ảnh nào vào nền của nút thì mọi thứ đều ổn và lưới trông giống như tôi muốn! – jiraya85

+0

Hmm được rồi, hãy thử với hình ảnh sau đó, chỉ cần chờ một vài, bị bệnh trở lại – FabianCook

2

enter image description here

Hãy thử sử dụng cái này:

https://rapidshare.com/files/2741870011/person.9.png

+0

Cảm ơn bạn rất nhiều! Điều này hoạt động hoàn hảo. Tôi thực sự đánh giá cao sự giúp đỡ của bạn! Rất rất cảm ơn!!! – jiraya85

+0

Bạn có cần những cái khác được thực hiện không? – FabianCook

+0

Tôi thực sự đánh giá cao sự giúp đỡ của bạn nhưng tôi không muốn làm phiền bạn! Bạn đã làm đủ rồi! Tôi sẽ tự mình làm chín bản vá. Cảm ơn bạn rất nhiều SmartLemon! – jiraya85

0

Khi tôi áp dụng hình nền như

android:background="@drawable/ic_launcher" 

nó làm việc tốt.

Tôi nghĩ đó là do kích thước của hình ảnh nên tôi đã áp dụng hình ảnh khác với kích thước 160X160 và sự cố đã xảy ra. Hơn cả tôi đã áp dụng cùng một hình ảnh 160X160 cho tất cả các nút và chúng được căn chỉnh hoàn hảo. đây là mã của tôi (Những hình ảnh Gender.png là 160x160 pixel)

<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

Tôi biết nó không phải là chính xác những gì bạn muốn, nhưng tôi khuyên bạn nên sử dụng GridView thay vì TableRow bố trí.

Tôi nghĩ GridView sẽ hoạt động tốt hơn vì vì nó được triển khai với chế độ xem tái chế và nội dung được kế thừa từ AbsListView. GridView khó triển khai hơn vì bạn phải sử dụng với Bộ điều hợp nhưng nó sẽ hoạt động hiệu quả nếu bạn có nhiều lượt xem nặng để tải, chẳng hạn như hình ảnh. 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> 
Các vấn đề liên quan