Den*_*ter 17 xml android android-linearlayout
我真的试过,但我无法理解Android如何解释layout_weight设置......
我想要实现的是
键入时我想将EditText增长到特定高度,并在输入的文本超出可用高度时开始滚动.这样做我需要周围的环境LinearLayout与EditText一起成长.
如果我为内部定义一个特定的高度,LinearLayout它将不会增长.如果我不这样做,内部布局占用所有空间而不是ScrollView,无论我尝试使用什么layout_weight.:(
我当前的XML看起来像这样:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="I'm a header" />
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:gravity="bottom">
<LinearLayout
android:id="@+id/itemContainer"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</ScrollView>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="50dp"
android:padding="2dp"
android:gravity="bottom"
android:isScrollContainer="true"
android:background="@drawable/steel" >
<EditText
android:id="@+id/edittext01"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="0.2"
android:scrollbars="vertical"
android:fadingEdge="vertical"
android:fadingEdgeLength="60dp"
android:maxHeight="40dp"
android:textSize="15sp" />
<Button
android:id="@+id/button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="0.8"
android:layout_gravity="right"
android:text="Send"
android:textStyle="bold"
android:textSize="15sp" />
</LinearLayout>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
任何提示都非常感谢!
Che*_*mon 37
layout_weight 用于确定Android在各种小部件获得所需空间之后如何划分任何剩余空间.
所以说你连续有3个小部件,每个小部件需要10个像素,但你有50个像素的空间.以下是一些layout_weights示例以及每个小部件声明的像素数:
widget1: weight = 1, 30px
widget2: weight = 0, 10px
widget3: weight = 0, 10px
widget1: weight = 1, 20px
widget2: weight = 1, 20px
widget3: weight = 0, 10px
widget1: weight = 1, 16.5px
widget2: weight = 1, 16.5px
widget3: weight = 1, 16.5px
Run Code Online (Sandbox Code Playgroud)
您可以将重量视为可用空间的百分比.它将累加所有值,然后根据需要给出部分.因此,如果它有帮助您可以使用最多100的权重来执行百分比:
widget1: weight = 0, 10px
widget2: weight = 25, 15px
widget3: weight = 75, 25px
Run Code Online (Sandbox Code Playgroud)
如果我理解正确,您希望底部窗口小部件以特定大小开始,然后根据需要扩展到某个最大大小,然后滚动.
并且,您希望它上面的小部件能够接管所有额外空间.
不幸的是,在Android中,它无法指定最大尺寸.我认为你能做的最好的事情就是给ScrollView一些固定的大小,layout_weight = 1并告诉底部的LinearLayout到wrap_content.这会(我认为)导致LinearLayout扩展,直到它不能再扩展,因为ScrollView已经声明了空间.
但是,challange指定了ScrollView的固定大小,它在所有不同的屏幕尺寸和分辨率下都有意义.您可能最终必须为不同的分辨率创建不同的布局,这可能不值得.就个人而言,我只是给editText一个固定的大小......