29

Android yüzde destek paketinden PercentRelativeLayout kullanıyorum. Benim düzenimde sahip olduğum şey bu.YüzdeRelativeLayout, programatik olarak yüksekliği ayarlama

<android.support.percent.PercentRelativeLayout 
    android:id="@+id/view_parent" 
    app:layout_heightPercent="68%" android:visibility="invisible" 
    android:layout_width="match_parent" 
    android:background="@color/mountain_meadow" android:layout_alignParentTop="true" 
    android:layout_height="wrap_content"> 


    <View android:id="@+id/view_child" android:layout_width="match_parent" app:layout_heightPercent="30%" 
     android:layout_alignParentBottom="true" 
     /> 

</android.support.percent.PercentRelativeLayout> 

programlı view_child yüksekliğini değiştirmek istiyorum. Bunu PercentRelativeLayout.LayoutParams veya başka herhangi bir yolu kullanarak nasıl yapabilirim.

cevap

65

Yeni bir yüzde değeri mi ayarlamak istiyorsunuz? Eğer evet ise, yapmanız gerekenler:

View view = findViewById(R.id.view_child); 
PercentRelativeLayout.LayoutParams params = (PercentRelativeLayout.LayoutParams) view.getLayoutParams(); 
// This will currently return null, if it was not constructed from XML. 
PercentLayoutHelper.PercentLayoutInfo info = params.getPercentLayoutInfo(); 
info.heightPercent = 0.60f; 
view.requestLayout(); 
+0

Yüzde değeri olmayan başka bir değer nasıl ayarlanır? örneğin, dp içinde marjBottom, Yüzde düzen paramsini kullanarak? – Gilian

+0

view.requestLayout(); havalı adam. – msecilmis

İlgili konular