2013-04-24 36 views
21

Tôi muốn đặt tỷ lệ phần trăm theo tỷ lệ phần trăm .. Tôi có 4 lần xem trong linearlayout và muốn đặt lề trái, phải, trên, dưới cùng mặc định giữ nguyên phần trăm cho mỗi kích thước màn hình.lề bố trí android với tỷ lệ phần trăm

là nó có thể?

đây là một bản demo những gì tôi muốn .. enter image description here

Và đây là những gì tôi đã cố gắng và không hoạt động

<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="match_parent" 
     android:layout_weight="1" 
     android:weightSum="10" > 

     <Thumbnail 
      android:id="@+id/thumb1" 
      android:layout_width="0dp" 
      android:layout_height="match_parent" 
      android:layout_weight="4" /> 

     <Thumbnail 
      android:id="@+id/thumb2" 
      android:layout_width="0dp" 
      android:layout_height="match_parent" 
      android:layout_weight="4" /> 

    </LinearLayout> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_weight="1" 
     android:weightSum="10" > 

     <Thumbnail 
      android:id="@+id/thumb3" 
      android:layout_width="0dp" 
      android:layout_height="match_parent" 
      android:layout_weight="4" > 
     </Thumbnail> 

     <Thumbnail 
      android:id="@+id/thumb4" 
      android:layout_width="0dp" 
      android:layout_height="match_parent" 
      android:layout_weight="4" /> 

    </LinearLayout> 

</LinearLayout> 

Nhờ sự giúp đỡ của bạn

Trả lời

22

Bạn có thể có vô hình View s trong LinearLayout s làm miếng đệm và sử dụng cơ chế layout_weight để gán cho chúng kích thước tương đối.

Ví dụ:

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_weight="1"> 

    <Thumbnail 
     android:id="@+id/thumb1" 
     android:layout_width="0dp" 
     android:layout_height="match_parent" 
     android:layout_weight="4" /> 

    <View 
     android:layout_width="0dp" 
     android:layout_height="0dp" 
     android:layout_weight="2" 
     android:visibility="invisible"/> 

    <Thumbnail 
     android:id="@+id/thumb2" 
     android:layout_width="0dp" 
     android:layout_height="match_parent" 
     android:layout_weight="4" /> 

</LinearLayout> 
+1

logic nếu không có bất kỳ cách nào khác :) nhưng không chắc chắn với việc sử dụng bộ nhớ, vì tôi cần rất nhiều quan điểm vô hình – dracula

+2

Có là một ' 'xem cho điều đó. Tôi không nghĩ rằng sẽ có bất kỳ hình phạt hiệu suất/bộ nhớ nào khi sử dụng 'Chế độ xem 'vô hình thay vào đó, nhưng thực sự không chắc chắn -' View.java' là rất lớn, đã lướt qua nó chỉ trong vài phút. –

+1

@AlexanderMalakhov 'Space' đã được thêm vào API cấp 14 nên không hữu ích. Đặt tên của nó làm cho mục đích rõ ràng hơn mặc dù. – laalto

0

Nhìn vào hình ảnh của bạn ở đây là những gì bạn có thể có thể làm, sử dụng một cách bố trí tương đối và đặt cách bố trí tuyến tính bên trong nó. bạn cũng có thể sử dụng weightSum nếu bạn muốn làm cho các khung nhìn hình ảnh vừa với nhau.

<RelativeLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_marginLeft="5dp" 
    android:layout_marginLeft="5dp" > 

    <LinearLayout 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:orientation="vertical" > 

     <ImageView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" /> 

     <ImageView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" /> 
    </LinearLayout> 

    <LinearLayout 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentRight="true" 
     android:orientation="vertical" > 

     <ImageView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" /> 

     <ImageView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" /> 
    </LinearLayout> 

</RelativeLayout> 
+0

cho lề chính xác không phải là cách tốt cho nhiều màn hình. đó là lý do tại sao tôi đang tìm giải pháp phần trăm – dracula

+0

Đặt lề trái và phải cho bố cục tương đối của bạn sẽ đặt cùng lề trên tất cả các màn hình vì các giá trị này nằm trong dp. – deepdroid

+0

nhưng giá trị dp ảnh hưởng khác nhau đối với từng mật độ. – dracula

19

Ngày 01 Tháng Tám năm 2017:

Hai bố trí từ câu trả lời này đang bị phản đối nhưng có một mô tả cung cấp có về cách để có được những chức năng tương tự với ConstraintLayout. Cảm ơn dpg vì đã chỉ ra điều này.

Nếu bạn định sử dụng tỷ lệ phần trăm với tài nguyên, this answer có thể hữu ích.


Cũ câu trả lời:

Hiện nay có một cách tốt hơn đi kèm với thư viện hỗ trợ phiên bản 23.0.0 (khoảng thời gian, phải không?). Bạn hiện có thể sử dụng PercentFrameLayout hoặc PercentRelativeLayout. Cả hai đều có các thuộc tính sau:

  • layout_widthPercent
  • layout_heightPercent
  • layout_marginPercent
  • layout_marginLeftPercent
  • layout_marginTopPercent
  • layout_marginRightPercent
  • layout_marginBottomPercent
  • layout_marginStartPe rcent
  • layout_marginEndPercent

Bạn cũng có thể có một cái nhìn tại PercentLayoutHelper.PercentLayoutParams

+0

Đây có phải chỉ dành cho Android 6 không? –

+1

Bố cục phần trăm nằm trong Thư viện Hỗ trợ Phần trăm, có nghĩa là chúng không chỉ dành cho Android 6. – neits

+1

Cách tốt hơn mới đã không còn được dùng nữa. Đối với cả hai Bố cục trên các liên kết: 'Lớp này không được chấp nhận ở cấp API 26.0.0-beta1. xem xét sử dụng ConstraintLayout và bố cục được liên kết thay thế.' – dpg

0

Bạn có thể thiết lập lề với tỷ lệ sử dụng ConstraintLayout's Guidelines.

Giả sử bạn muốn xác định giá trị phần trăm sau cho bố trí của bạn:

Requested percentage values

Sau đó, bạn chỉ cần thêm hướng dẫn để bố trí của bạn như sau:

Layout Editor

<?xml version="1.0" encoding="utf-8"?> 
<android.support.constraint.ConstraintLayout 
    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="match_parent"> 

    <ImageView 
     android:id="@+id/imageView1" 
     android:layout_width="0dp" 
     android:layout_height="0dp" 
     android:scaleType="centerCrop" 
     app:srcCompat="@drawable/ic_launcher_background" 
     app:layout_constraintTop_toTopOf="@+id/horGuideline1" 
     app:layout_constraintStart_toStartOf="@+id/verGuideline1" 
     app:layout_constraintEnd_toStartOf="@+id/verGuideline2" 
     app:layout_constraintBottom_toTopOf="@+id/horGuideline2" /> 

    <ImageView 
     android:id="@+id/imageView2" 
     android:layout_width="0dp" 
     android:layout_height="0dp" 
     android:scaleType="centerCrop" 
     app:srcCompat="@drawable/ic_launcher_background" 
     app:layout_constraintTop_toTopOf="@+id/horGuideline1" 
     app:layout_constraintStart_toStartOf="@+id/verGuideline3" 
     app:layout_constraintEnd_toStartOf="@+id/verGuideline4" 
     app:layout_constraintBottom_toTopOf="@+id/horGuideline2" /> 

    <ImageView 
     android:id="@+id/imageView3" 
     android:layout_width="0dp" 
     android:layout_height="0dp" 
     android:scaleType="centerCrop" 
     app:srcCompat="@drawable/ic_launcher_background" 
     app:layout_constraintTop_toTopOf="@+id/horGuideline3" 
     app:layout_constraintStart_toStartOf="@+id/verGuideline1" 
     app:layout_constraintEnd_toStartOf="@+id/verGuideline2" 
     app:layout_constraintBottom_toTopOf="@+id/horGuideline4" /> 

    <ImageView 
     android:id="@+id/imageView4" 
     android:layout_width="0dp" 
     android:layout_height="0dp" 
     android:scaleType="centerCrop" 
     app:srcCompat="@drawable/ic_launcher_background" 
     app:layout_constraintTop_toTopOf="@+id/horGuideline3" 
     app:layout_constraintStart_toStartOf="@+id/verGuideline3" 
     app:layout_constraintEnd_toStartOf="@+id/verGuideline4" 
     app:layout_constraintBottom_toTopOf="@+id/horGuideline4" /> 

    <android.support.constraint.Guideline 
     android:id="@+id/verGuideline1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:orientation="vertical" 
     app:layout_constraintGuide_percent="0.05" /> 

    <android.support.constraint.Guideline 
     android:id="@+id/verGuideline2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:orientation="vertical" 
     app:layout_constraintGuide_percent="0.35" /> 

    <android.support.constraint.Guideline 
     android:id="@+id/verGuideline3" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:orientation="vertical" 
     app:layout_constraintGuide_percent="0.65" /> 

    <android.support.constraint.Guideline 
     android:id="@+id/verGuideline4" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:orientation="vertical" 
     app:layout_constraintGuide_percent="0.95" /> 

    <android.support.constraint.Guideline 
     android:id="@+id/horGuideline1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal" 
     app:layout_constraintGuide_percent="0.12" /> 

    <android.support.constraint.Guideline 
     android:id="@+id/horGuideline2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal" 
     app:layout_constraintGuide_percent="0.47" /> 

    <android.support.constraint.Guideline 
     android:id="@+id/horGuideline3" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal" 
     app:layout_constraintGuide_percent="0.53" /> 

    <android.support.constraint.Guideline 
     android:id="@+id/horGuideline4" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal" 
     app:layout_constraintGuide_percent="0.88" /> 

</android.support.constraint.ConstraintLayout> 

Kết quả là bố cục của bạn trông giống như sau:

Result view

Các vấn đề liên quan