Android 按百分比(或类似值)动态调整视图大小?
举个例子:假设我有两个s和一个按钮,我用它作为登录表单。我希望EditText大小相同,一个接一个,登录宽度为其宽度的一半。大概是这样的:Android 按百分比(或类似值)动态调整视图大小?,android,user-interface,Android,User Interface,举个例子:假设我有两个s和一个按钮,我用它作为登录表单。我希望EditText大小相同,一个接一个,登录宽度为其宽度的一半。大概是这样的: <TableLayout android:stretchColumns="0,1" android:shrinkColumns="0,1" android:layout_width="fill_parent" android:layout_height="wrap_content
<TableLayout
android:stretchColumns="0,1"
android:shrinkColumns="0,1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@id/login_EditText_password">
<TableRow>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<Button
android:text="@string/login_login"
android:id="@+id/login_Button_login"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</TableRow>
</TableLayout>
我能找到的使按钮宽度为1/2(但仍保持动态大小)的唯一方法是使用具有空视图的作为第一个字段。大概是这样的:
<TableLayout
android:stretchColumns="0,1"
android:shrinkColumns="0,1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@id/login_EditText_password">
<TableRow>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<Button
android:text="@string/login_login"
android:id="@+id/login_Button_login"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</TableRow>
</TableLayout>
这感觉像是一个可怕的黑客,必须有更好的方法。你知道其中一个吗?通常使用
布局\重量
来实现。但这通常在父视图中。在这种情况下,您可以创建一个空视图来占据另一半的空间,但希望按钮的宽度为上述字段的一半是一个奇怪的用例
编辑,下面是一个示例:
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@id/login_EditText_password"
android:orientation="horizontal">
<View
android:layout_width="0dip"
android:layout_height="0dip"
android:layout_weight="1" />
<Button
android:text="@string/login_login"
android:id="@+id/login_Button_login"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1" />
</LinearLayout>
android:layou-weight
只能在一行或一列中工作。带有权重1的空视图和权重1按钮的水平线性布局
基本上就是这个解决方案所做的,但更轻更漂亮。是的。。。那有点干净。我将编辑您的答案以提供代码。我不敢相信我错过了。事实上,这是一个有趣的解决方案,我不确定有没有更好的选择。您可以使用普通的视图
而不是文本视图
,以获得稍微好一点的性能。