부모 레이아웃의 백분율로 너비를 동적으로 변경하려는 간단한 진행 막대보기가 있습니다.동적으로 뷰 레이아웃을 부모 레이아웃의 백분율로 설정합니다.
최소 너비가 설정된 api가 있습니다. 그러나 전달할 정확한 숫자는 무엇입니까? 내가 부모 너비를 가져 와서 아이의 백분율과 너비를 계산하고 minimumWidth로 전달해야합니다.
또는 xml 레이아웃 코드로 달성 할 수있는 것입니까?
부모 레이아웃의 백분율로 너비를 동적으로 변경하려는 간단한 진행 막대보기가 있습니다.동적으로 뷰 레이아웃을 부모 레이아웃의 백분율로 설정합니다.
최소 너비가 설정된 api가 있습니다. 그러나 전달할 정확한 숫자는 무엇입니까? 내가 부모 너비를 가져 와서 아이의 백분율과 너비를 계산하고 minimumWidth로 전달해야합니다.
또는 xml 레이아웃 코드로 달성 할 수있는 것입니까?
이제
ProgressBar view_instance = (ProgressBar)findViewById(R.id.nutrition_bar_filled);
LayoutParams params=view_instance.getLayoutParams();
params.width=newOne;
view_instance.setLayoutParams(params);
, 당신의 XML은 다음과 같습니다 말할 수 PercentRelativeLayout과 같은 android.support.percent.* 패키지의 수업은 현재 사용되지 않습니다.
이 클래스는 API 레벨 26.1.0에서 사용되지 않습니다.
대신 ConstraintLayout 및 관련 레이아웃을 사용하는 것이 좋습니다. 다음은 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>
그냥 사용을 ConstraintLayout 예
다음과 같이이하는 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
지원 라이브러리를 포함해야합니다. 이렇게하려면, 이것은하지 XML에서, 코드에서 수행 할 수 sould 당신의 build.gradle
파일
compile 'com.android.support:percent:{lastest_support_library_version}'
에 다음 줄을 포함한다. 코드 조각에서
봐 :
<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);