Android 以父布局的百分比动态设置视图宽度
我有一个简单的进度条视图,我想动态更改宽度占父布局的百分比 我看到有一套最小宽度api。但是要传递的正确数字是多少?我应该得到父级宽度,然后计算子级的百分比和宽度,并将其作为minimumWidth传递Android 以父布局的百分比动态设置视图宽度,android,android-layout,android-constraintlayout,Android,Android Layout,Android Constraintlayout,我有一个简单的进度条视图,我想动态更改宽度占父布局的百分比 我看到有一套最小宽度api。但是要传递的正确数字是多少?我应该得到父级宽度,然后计算子级的百分比和宽度,并将其作为minimumWidth传递 或者,仅使用xml布局代码就可以实现这一点吗?这可以通过在线性布局中使用权重来实现,如下所示: <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" andro
或者,仅使用xml布局代码就可以实现这一点吗?这可以通过在
线性布局中使用权重来实现,如下所示:
<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>
<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>
通过在线性布局
中使用权重,无需任何代码即可实现这一点,如下所示:
<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>
<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>
这应该从代码而不是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);
这应该从代码而不是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);
来自包(如)的类当前已弃用
该类在API级别26.1.0中被弃用。
考虑使用约束布局和相关布局。下面显示了如何使用ConstraintLayout复制百分比布局的功能。准则用于定义每个百分比断点,然后拉伸按钮视图以填充间隙:
仅用于
例如:
<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级别26.1.0中被弃用。
考虑使用约束布局和相关布局。下面显示了如何使用ConstraintLayout复制百分比布局的功能。准则用于定义每个百分比断点,然后拉伸按钮视图以填充间隙:
仅用于
例如:
<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>
不幸的是,如果父级具有layout\u width=“wrap\u content”
,则无法正常工作。您知道如何使视图的宽度为父视图的70%,以便我可以在水平回收器视图中使用它吗?不幸的是,如果父视图具有layout\u width=“wrap\u content”
,这将无法正常工作。您知道如何使视图的宽度为父视图的70%,以便我可以在水平回收器视图中使用它吗?