2011-05-13 40 views
25

Android có hỗ trợ% hay không có cách nào gần đúng. Tôi có hai kích thước màn hình rất khác nhau và tôi nhưng tỷ lệ phần trăm khôn ngoan tôi muốn EditBox trong hoạt động có cùng lề cho cả hai kích thước màn hình như một tỷ lệ kích thước màn hình. Điều này có thể giải quyết như thế nào.Android làm cách nào để đặt tỷ lệ phần trăm/lề để EditText có 10% lợi nhuận ở hai bên?

Trả lời

32

Nó không thực sự hỗ trợ giá trị cài đặt theo phần trăm (ngoại trừ một số tệp hoạt ảnh xml dường như) Nếu bạn đã chết bằng cách sử dụng tỷ lệ phần trăm, cách tốt nhất tôi có thể nghĩ là từ cuộc gọi java getWidth và getHeight nhân các số thập phân của bạn và đặt kết quả bằng setMargin() hoặc setPadding().

29

Điều này có thể trong XML bằng cách gói EditText của bạn bên trong của một LinearLayout:

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:weightSum="10" 
    android:gravity="center" 
    >  

    <EditText 
     android:layout_height="wrap_content" 
     android:layout_width="0dip" 
     android:layout_weight="8" 
     /> 

</LinearLayout> 
+0

và cho ´height´ ¿? –

+0

nó sẽ không hoạt động nếu bạn chỉ có 1 thành phần trong 'LinearLayout'. – Raptor

+0

điều này làm việc cho tôi chỉ với một ListView trong LinearLayout và có hoặc không có bổ sung 'android: orientation =" horizontal "' trong LinearLayout với 'minSdkVersion = 16'. Đối với chiều cao, bạn sẽ chỉ có layout_height = – rockhammer

22

Edit: Các Layouts từ câu trả lời này đang bị phản đối. Sử dụng giải pháp mà Eugene Brusov đề xuất. Ngoài ra, cảm ơn bạn chancyWu cho nhận xét.


Hiện tại, có một cách tốt hơn với thư viện hỗ trợ phiên bản 23.0.0 (khoảng thời gian, đúng không?). Bạn hiện có thể sử dụng PercentFrameLayout hoặc PercentRelativeLayout.

Nếu bạn muốn EditText là 80% chiều rộng của màn hình với một biên độ 10% ở hai bên, mã sẽ trông 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="match_parent"> 

    <EditText 
     android:layout_height="wrap_content" 
     app:layout_widthPercent="80%" 
     app:layout_marginStartPercent="10%" 
     app:layout_marginEndPercent="10%"/> 

</android.support.percent.PercentRelativeLayout> 

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

+0

Không được chấp nhận vì cấp API 26.1.0 – chancyWu

1

Có thể với Guideline được giới thiệu trong ConstraintLayout.

Ví dụ bạn có thể đặt EditText hàng đầu của bạn ở mức 25% chiều cao màn hình và trái và phải ở mức 20% của chiều rộng màn hình:

Layout Editor

Đây là nguồn bố trí:

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

    <EditText 
     android:id="@+id/editText" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:ems="10" 
     android:inputType="textPersonName" 
     android:text="Name" 
     app:layout_constraintLeft_toLeftOf="@+id/left_guideline" 
     app:layout_constraintRight_toLeftOf="@+id/right_guideline" 
     app:layout_constraintTop_toTopOf="@+id/top_guideline" /> 

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

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

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

</android.support.constraint.ConstraintLayout> 
Các vấn đề liên quan