如何在运行时缩放 Android 布局对象?
我想在 UI 初始化中动态将 LinearLayout 的宽度设置为屏幕宽度的一半。我有一个 RelativeLayout
包裹在 LinearLayout
周围,层次结构如下:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:id="@+id/left_linear_layout"
android:layout_alignParentLeft="true"
android:layout_height="fill_parent"
android:layout_width="155dp" <!--want to set this to 1/2 screen width-->
android:orientation="vertical">
...
</LinearLayout>
<LinearLayout
android:id="@+id/right_linear_layout"
android:layout_alignParentRight="true"
android:layout_height="fill_parent"
android:layout_width="385dp"><!--want to set this relative to screen width as well-->
....
</LinearLayout>
</RelativeLayout>
或者,可以使用 View
而不是 来解决此问题布局?任何建议表示赞赏!
I want to set the width of a LinearLayout
to half of the screen width dynamically in my UI initialization. I have a RelativeLayout
wrapped around the LinearLayout
, the hierarchy is the following:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:id="@+id/left_linear_layout"
android:layout_alignParentLeft="true"
android:layout_height="fill_parent"
android:layout_width="155dp" <!--want to set this to 1/2 screen width-->
android:orientation="vertical">
...
</LinearLayout>
<LinearLayout
android:id="@+id/right_linear_layout"
android:layout_alignParentRight="true"
android:layout_height="fill_parent"
android:layout_width="385dp"><!--want to set this relative to screen width as well-->
....
</LinearLayout>
</RelativeLayout>
Alternatively, can this problem be solved using View
instead of Layout
? Any suggestion is appreciated!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用
layout_weight
来实现此目的,但您需要添加一些不可见的视图进行填充。例如,以下内容将使您的顶部面板成为屏幕宽度的一半:每个视图占用的量为
layout_weight/total_layout_weight
。在本例中,total_layout_weight = 1+1 = 2
并且每个视图的layout_weight
为 1,因此每个视图占用1/2
的屏幕。You can do this by using
layout_weight
, but you'll need to add some invisible views for padding. For instance the following would make your top panel half the screen width:The amount each view will take up is
layout_weight/total_layout_weight
. In this casetotal_layout_weight = 1+1 = 2
and each view has alayout_weight
of 1, so each view takes up1/2
of the screen.您可以简单地使用 LinearLayout 作为顶级布局,然后设置两个子布局的权重。
You could simply use a LinearLayout as your top-level layout, and then set the weight of the two child layouts.