将视图宽度动态设置为父布局的百分比
Dynamically setting view width as a percentage of parent layout
我有一个简单的进度条视图,我想按父布局的百分比动态更改宽度。
我看到设置了最小宽度 api。但是要传入的正确数字是多少?我是否应该获取父级宽度,然后只计算子级的百分比和宽度并将其作为 minimumWidth 传递。
或者,这是否可以仅使用 xml 布局代码来实现?
通过在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}'
这应该通过代码完成,而不是通过 xml。
查看代码片段:
ProgressBar view_instance = (ProgressBar)findViewById(R.id.nutrition_bar_filled);
LayoutParams params=view_instance.getLayoutParams();
params.width=newOne;
view_instance.setLayoutParams(params);
现在,假设您的 xml 看起来像这样:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
您将像这样使用上面的代码:
ProgressBar view_instance = (ProgressBar)findViewById(R.id.nutrition_bar_filled);
LinearLayout.LayoutParams params=(LinearLayout.LayoutParams)view_instance.getLayoutParams();
params.width= yourLinearLayout.getWidth() / 100 * percent;
view_instance.setLayoutParams(params);
类 来自 android.support.percent.* package such as PercentRelativeLayout,目前已弃用
This class was deprecated in API level 26.1.0.
consider using ConstraintLayout and associated layouts instead. The following shows how to replicate the functionality of percentage layouts with a ConstraintLayout. The Guidelines are used to define each percentage break point, and then a Button view is stretched to fill the gap:
只需使用ConstraintLayout with GuideLines
示例:
<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">
<android.support.constraint.Guideline
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/left_guideline"
app:layout_constraintGuide_percent=".15"
android:orientation="vertical"/>
<android.support.constraint.Guideline
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/right_guideline"
app:layout_constraintGuide_percent=".85"
android:orientation="vertical"/>
<android.support.constraint.Guideline
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/top_guideline"
app:layout_constraintGuide_percent=".15"
android:orientation="horizontal"/>
<android.support.constraint.Guideline
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/bottom_guideline"
app:layout_constraintGuide_percent=".85"
android:orientation="horizontal"/>
<Button
android:text="Button"
android:layout_width="0dp"
android:layout_height="0dp"
android:id="@+id/button"
app:layout_constraintLeft_toLeftOf="@+id/left_guideline"
app:layout_constraintRight_toRightOf="@+id/right_guideline"
app:layout_constraintTop_toTopOf="@+id/top_guideline"
app:layout_constraintBottom_toBottomOf="@+id/bottom_guideline" />
</android.support.constraint.ConstraintLayout>
我有一个简单的进度条视图,我想按父布局的百分比动态更改宽度。
我看到设置了最小宽度 api。但是要传入的正确数字是多少?我是否应该获取父级宽度,然后只计算子级的百分比和宽度并将其作为 minimumWidth 传递。
或者,这是否可以仅使用 xml 布局代码来实现?
通过在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}'
这应该通过代码完成,而不是通过 xml。
查看代码片段:
ProgressBar view_instance = (ProgressBar)findViewById(R.id.nutrition_bar_filled);
LayoutParams params=view_instance.getLayoutParams();
params.width=newOne;
view_instance.setLayoutParams(params);
现在,假设您的 xml 看起来像这样:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
您将像这样使用上面的代码:
ProgressBar view_instance = (ProgressBar)findViewById(R.id.nutrition_bar_filled);
LinearLayout.LayoutParams params=(LinearLayout.LayoutParams)view_instance.getLayoutParams();
params.width= yourLinearLayout.getWidth() / 100 * percent;
view_instance.setLayoutParams(params);
类 来自 android.support.percent.* package such as PercentRelativeLayout,目前已弃用
This class was deprecated in API level 26.1.0.
consider using ConstraintLayout and associated layouts instead. The following shows how to replicate the functionality of percentage layouts with a ConstraintLayout. The Guidelines are used to define each percentage break point, and then a Button view is stretched to fill the gap:
只需使用ConstraintLayout with GuideLines
示例:
<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">
<android.support.constraint.Guideline
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/left_guideline"
app:layout_constraintGuide_percent=".15"
android:orientation="vertical"/>
<android.support.constraint.Guideline
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/right_guideline"
app:layout_constraintGuide_percent=".85"
android:orientation="vertical"/>
<android.support.constraint.Guideline
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/top_guideline"
app:layout_constraintGuide_percent=".15"
android:orientation="horizontal"/>
<android.support.constraint.Guideline
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/bottom_guideline"
app:layout_constraintGuide_percent=".85"
android:orientation="horizontal"/>
<Button
android:text="Button"
android:layout_width="0dp"
android:layout_height="0dp"
android:id="@+id/button"
app:layout_constraintLeft_toLeftOf="@+id/left_guideline"
app:layout_constraintRight_toRightOf="@+id/right_guideline"
app:layout_constraintTop_toTopOf="@+id/top_guideline"
app:layout_constraintBottom_toBottomOf="@+id/bottom_guideline" />
</android.support.constraint.ConstraintLayout>