子元素的高度作为父元素的百分比

时间:2014-05-08 17:28:13

标签: android

我在EditText内有一个LinearLayout标签,水平方向,我希望将EditText的高度作为父LinearLayout's高度的百分比。如何实现这一目标?

请不要建议layout_weight属性,因为它将用于控制子元素的宽度而不是高度。

2 个答案:

答案 0 :(得分:1)

您可以编写一个自定义布局,将其子级调整为正确的百分比,这可能是最佳解决方案。

或者你可以使用layout_weight并将EditText包装在另一个LinearLayout中,例如:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:weightSum="2"
        android:orientation="vertical">
        <View
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_weight="1"/>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:text="one"/>
    </LinearLayout>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="two"/>
</LinearLayout>

答案 1 :(得分:0)

LinearLayout中的百分比非常简单。

给你的观点一个重量

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:weightSum="2"
    android:orientation="vertical">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_weight="0.5"/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_weight="0.25"
        android:text="one"/>
</LinearLayout>

我已将两个textViews的高度设置为0,但layout_weight表示要生成一个50%的LinearLayout,另外25%表示布局的高度。

相关问题