2013-05-09 15 views
10

Tôi đang cố gắng tạo bố cục với 6 nút tự động điều chỉnh kích thước màn hình dưới dạng các ô cửa sổ của điện thoại. Trong mã tôi tạo động 6 nút, 2 cho dòng nhưng nút nên phù hợp với kích thước của màn hình điền sau này. làm thế nào tôi có thể tiến hành?Cách tạo bố cục với 6 nút như các ô cửa sổ

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="vertical" > 

<LinearLayout 
    android:layout_width="match_parent" 
     android:layout_height="0dip" 
    android:orientation="horizontal" 
    android:weightSum="2" > 

    <Button 
     android:layout_width="0dip" 
       android:layout_height="match_parent" 
     android:layout_weight="1" 
     android:background="@drawable/conv_up" /> 

    <Button 
     android:layout_width="0dip" 
       android:layout_height="match_parent" 
     android:layout_weight="1" 
     android:background="@drawable/conv_up" 
     /> 

</LinearLayout> 

<LinearLayout 
    android:layout_width="match_parent" 
     android:layout_height="0dip" 
    android:orientation="horizontal" 
    android:weightSum="2" > 

    <Button 
     android:layout_width="0dip" 
       android:layout_height="match_parent" 
     android:layout_weight="1" 
     android:background="@drawable/conv_up" 
     /> 

    <Button 
     android:layout_width="0dip" 
       android:layout_height="match_parent" 
     android:layout_weight="1" 
     android:background="@drawable/conv_up" 
     /> 

</LinearLayout> 

<LinearLayout 
    android:layout_width="match_parent" 
     android:layout_height="0dip" 
    android:orientation="horizontal" 
    android:weightSum="2" > 

    <Button 
     android:layout_width="0dip" 
       android:layout_height="match_parent" 
     android:layout_weight="1" 
     android:background="@drawable/conv_up" 
     /> 

    <Button 
     android:layout_width="0dip" 
       android:layout_height="match_parent" 
     android:layout_weight="1" 
     android:background="@drawable/conv_up" 
     /> 

</LinearLayout> 

+0

Đặt những gì bạn thấy bây giờ và những gì bạn mong đợi. Khác sẽ rất khó để trả lời. – Siddharth

+0

Tôi không thể nhìn thấy bất cứ điều gì vì mã này cho tôi lỗi này: "Kích thước đáng ngờ: điều này sẽ làm cho chế độ xem ẩn, nên được sử dụng với layout_weight" trên LinearLayout android: layout_height = "0dip" – bisemanu

+0

'0dip' là vấn đề. – Siddharth

Trả lời

16

Tôi muốn sử dụng một dọc LinearLayout với ba hàng của cùng một trọng lượng như trẻ em, mỗi hàng là một ngang LinearLayout có hai đứa con của cùng trọng lượng, sẽ đảm bảo toàn bộ khu vực được lấp đầy. Đối với sáu nút hiệu suất không phải là một vấn đề.

Nếu hiệu suất là mối quan tâm, bạn có thể tạo các hàng là RelativeLayout s và sử dụng thanh chống để chia đôi và định vị hai trẻ dựa trên đó.

Khi tôi nói một strut, tôi muốn nói điều này:

<View android:id="@+id/strut" 
    android:layout_width="0dp" 
    android:layout_height="0dp" 
    android:layout_centerHorizontal="true"/> 

Cập nhật: Vì bạn đang cố gắng các LinearLayout s, dưới đây là cách bạn có thể đối phó với chiều cao và độ rộng:

Phụ huynh LinearLayout có thể có:

android:layout_width="match_parent" 
android:layout_height="match_parent" 

Ba LinearLayout trẻ em sẽ có:

android:layout_width="match_parent" 
android:layout_height="0dip" 

Các Button s sẽ có:

android:layout_width="0dip" 
android:layout_height="match_parent" 

Như bạn có thể thấy, chúng ta có 0dip cho các tài sản mà trọng lượng được áp dụng trên (hoặc chiều cao nếu cha mẹ là theo chiều dọc, hoặc chiều rộng nếu cha mẹ theo chiều ngang), mà sẽ cần phải phát triển để điền vào không gian.

Dưới đây là toàn bộ XML (các nút không bao gồm drawables, vì vậy cảm thấy tự do để thêm bạn):

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" > 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="0dip" 
     android:orientation="horizontal" 
     android:layout_weight="1" > 

     <Button 
      android:layout_width="0dip" 
      android:layout_height="match_parent" 
      android:layout_weight="1" /> 

     <Button 
      android:layout_width="0dip" 
      android:layout_height="match_parent" 
      android:layout_weight="1"/> 
    </LinearLayout> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="0dip" 
     android:orientation="horizontal" 
     android:layout_weight="1" > 

     <Button 
      android:layout_width="0dip" 
      android:layout_height="match_parent" 
      android:layout_weight="1" /> 

     <Button 
      android:layout_width="0dip" 
      android:layout_height="match_parent" 
      android:layout_weight="1"/> 
    </LinearLayout> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="0dip" 
     android:orientation="horizontal" 
     android:layout_weight="1" > 

     <Button 
      android:layout_width="0dip" 
      android:layout_height="match_parent" 
      android:layout_weight="1" /> 

     <Button 
      android:layout_width="0dip" 
      android:layout_height="match_parent" 
      android:layout_weight="1" /> 
    </LinearLayout> 
</LinearLayout> 

Và kết quả: enter image description here

+0

Tôi đã chỉnh sửa câu hỏi của mình bằng mã xml. Nhưng với mã này, tôi gặp vấn đề với việc hiển thị các nút không được hiển thị chính xác trên màn hình – bisemanu

+0

'android: layout_width =" 0dip "' là những gì bạn cần cho tất cả độ rộng của các nút 'Nút' đó. – Voicu

+0

tôi đã chỉnh sửa câu hỏi của mình nhưng không hoạt động theo cách – bisemanu

3

Tôi nghĩ bạn nên có một cái nhìn tại GridView

+0

Gạch Windows cũng có ô phân tách, GridView sẽ trợ giúp như thế nào? – Siddharth

0

Sử dụng GridLayout! Điều này là hoàn hảo cho tình huống này

<GridLayout 
    android:layout_width="match_parent" 
    android:layout_height="350dp" 
    android:layout_margin="0.5dp" 
    android:columnCount="2" 
    android:rowCount="3" > 

    <Button 
     android:id="@+id/b_1" 
     /> 

    <Button 
     android:id="@+id/b_2" 
     /> 

    <Button 
     android:id="@+id/b_3" 
     /> 

    <Button 
     android:id="@+id/b_4" 
     /> 

    <Button 
     android:id="@+id/b_5" 
     /> 

    <Button 
     android:id="@+id/b_6" 
     /> 
</GridLayout> 
0

Tôi đang sử dụng thư viện Android .v7. Xml này đã làm việc cho tôi để tạo 2 cột, bố cục 3 hàng lấp đầy toàn bộ màn hình:

<?xml version="1.0" encoding="utf-8"?> 
<android.support.v7.widget.GridLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/lib/android.support.v7.widget.GridLayout" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    xmlns:grid="http://schemas.android.com/apk/res-auto" 
    android:layout_margin="0.5dp" 
    app:columnCount="2" 
    app:rowCount="3" 
    app:useDefaultMargins="true"> 

    <Button 
     android:id="@+id/b_1" 
     grid:layout_columnWeight="1" 
     grid:layout_rowWeight="1" 
     grid:layout_row="0" 
     grid:layout_column="0" 
     android:text="Hellooo"/> 

    <Button 
     android:id="@+id/b_2" 
     grid:layout_columnWeight="1" 
     grid:layout_rowWeight="1" 
     grid:layout_row="0" 
     grid:layout_column="1" 
     android:text="Hellooo"/> 

    <Button 
     android:id="@+id/b_3" 
     grid:layout_columnWeight="1" 
     grid:layout_rowWeight="1" 
     grid:layout_row="1" 
     grid:layout_column="0" 
     android:text="Hellooo"/> 

    <Button 
     android:id="@+id/b_4" 
     grid:layout_columnWeight="1" 
     grid:layout_rowWeight="1" 
     grid:layout_row="1" 
     grid:layout_column="1" 
     android:text="Hellooo"/> 

    <Button 
     android:id="@+id/b_5" 
     grid:layout_columnWeight="1" 
     grid:layout_rowWeight="1" 
     grid:layout_row="2" 
     grid:layout_column="0" 
     android:text="Hellooo"/> 

    <Button 
     android:id="@+id/b_6" 
     grid:layout_columnWeight="1" 
     grid:layout_rowWeight="1" 
     grid:layout_row="2" 
     grid:layout_column="1" 
     android:text="Hellooo"/> 

</android.support.v7.widget.GridLayout>