2016-11-30 37 views
1

我有一個簡單的進度欄視圖,我想動態更改寬度作爲父佈局的百分比。動態設置視圖寬度作爲父佈局的百分比

我看到有一組minimumWidth API。但是傳入的正確數字是什麼?我應該得到父寬度,然後只計算孩子的百分比和寬度,並將其作爲minimumWidth傳遞。

或者,這是隻有XML佈局代碼可實現的東西嗎?

回答

2

android.support.percent.*包類如PercentRelativeLayout,目前已棄用

該類在API層次26.1.0棄用。
請考慮使用ConstraintLayout和相關佈局。以下顯示如何使用ConstraintLayout複製百分比佈局的功能。該準則被用來定義每個百分比斷點,然後一個按鈕視圖拉伸以填充間隙:

只要使用ConstraintLayoutGuideLines

實施例:

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

這是不通過使用在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}' 
0

以下行這從前人的精力代碼來完成,而不是從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); 
相關問題