2013-03-04 7 views
6

Tôi cần đặt chiều rộng của chế độ xem là 50% chiều rộng của màn hình và sau đó căn giữa chế độ xem này theo chiều ngang trong khi có khả năng có 1 hoặc nhiều nút có thể xuất hiện ở bên trái hoặc bên phải của màn hình.Bố cục chiều rộng phần trăm Android

Tôi đang sử dụng bố cục tương đối để tôi có thể đặt bố cục tuyến tính với trọng số để lấy 50% trọng tâm trong khi đặt bất kỳ nút nào lên trên LL được gắn vào cạnh trái hoặc phải của RL. Tuy nhiên bố cục này thiếu thanh giữa màu xanh. Nếu tôi đặt layout_weight ở giữa là 1, tôi nhận được 3 thanh có kích thước bằng nhau.

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

Trả lời

15

Bạn nên thiết lập chiều rộng tầm nhìn để 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

Đây là cách 'công trình layout_weight'. 'layout_weight' cho biết cách ** không gian còn lại ** trong parent' View' sẽ bị lệch bởi con 'View'. Trong trường hợp của bạn đầu tiên 'View' có' android: layout_width' được đặt thành 'fill_parent' để không có bất kỳ khoảng trống còn lại nào. Tôi không thực sự biết lý do tại sao nhìn cuối cùng vẫn còn nhìn thấy được, bởi vì tôi đã phát hiện ra chỉ để xem cái đầu tiên. –

-2

Nếu tôi hiểu đúng, bạn cần điều này:

<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

Bạn có thể đạt được điều này với thư viện phần trăm mới:

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

bằng cách làm một cái gì đó như thế này:

<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

Sử dụng thư viện mới hỗ trợ tỷ lệ phần trăm.

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

Ví dụ:

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

Để biết thêm thông tin Android Doc

* Đối với mẫu & Tutorial * Tutorial1Tutorial2Tutorial3