Это достижимо без какого-либо кода с использованием весов в LinearLayout
, следующим образом:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="10"
android:orientation="horizontal">
<ProgressBar
android:layout_widthPercent="0dp"
android:layout_heightPercent="wrap_content"
android:weight="1"/>
</LinearLayout>
И потому, что родитель имеет общую сумму 10 единиц, и ребенок будет принимать только 1 из них, ребенок фактически будет иметь 10% от ширины родительского элемента.
Также возможно использовать мощность библиотеки поддержки через PercentRelativeLayout. Основное применение заключается в следующем:
<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">
<ProgressBar
app:layout_widthPercent="50%"
app:layout_heightPercent="wrap_content"
app:layout_marginTopPercent="10%"/>
</android.support.percent.PercentRelativeLayout>
Чтобы добавить его в свой проект, вам нужно будет включать в себя библиотеку поддержки percent
в вашем проекте. Для этого, включите следующую строку в файл build.gradle
compile 'com.android.support:percent:{lastest_support_library_version}'