29

सेट करने के लिए मैं एंड्रॉइड प्रतिशत समर्थन पैकेज से PercentRelativeLayout का उपयोग कर रहा हूं। मेरे पास मेरे लेआउट में यही है।प्रतिशत रिटेलिवेट, ऊंचाई को प्रोग्रामेटिक रूप से

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

मैं प्रोग्राम के रूप में view_child की ऊंचाई को बदलना चाहते हैं। मैं PercentRelativeLayout.LayoutParams या किसी अन्य तरीके से इसका उपयोग कैसे कर सकता हूं।

उत्तर

65

क्या आप एक नया प्रतिशत मूल्य निर्धारित करना चाहते हैं? यदि हां, तो आपको:

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

प्रतिशत के बिना एक और मूल्य कैसे निर्धारित किया जाए? उदाहरण के लिए, प्रतिशत लेआउट पैरा का उपयोग करके डीपी में मार्जिनबॉटम? – Gilian

+0

view.requestLayout(); शांत लड़का। – msecilmis

संबंधित मुद्दे