如何在运行时缩放Android布局对象?
我想在UI初始化中动态地将LinearLayout的宽度设置为屏幕宽度的一半。我在LinearLayout周围有一个RelativeLayout,层次结构如下:如何在运行时缩放Android布局对象?,android,android-layout,layout,runtime,scale,Android,Android Layout,Layout,Runtime,Scale,我想在UI初始化中动态地将LinearLayout的宽度设置为屏幕宽度的一半。我在LinearLayout周围有一个RelativeLayout,层次结构如下: <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" androi
<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>
或者,是否可以使用视图而不是布局来解决此问题?任何建议都将不胜感激 您可以简单地使用线性布局作为顶层布局,然后设置两个子布局的权重。您可以通过使用布局权重来实现这一点,但您需要添加一些不可见的填充视图。例如,以下情况将使顶部面板的屏幕宽度减半:
<RelativeLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout
android:id="@+id/left_linear_layout"
android:layout_alignParentLeft="true"
android:layout_height="fill_parent"
android:layout_width="0dp"
android:layout_weight="1"
android:orientation="vertical"
>
...
</LinearLayout>
<!-- need this view to fill the other half of the screen -->
<View
android:id="@+id/spacer"
android:layout_toRightOf="@id/left_linear_layout"
android:layout_height="fill_parent"
android:layout_width="0dp"
android:layout_weight="1"
/>
....
</RelativeLayout>
每个视图将占用的量为布局重量/总布局重量。在这种情况下,总布局权重=1+1=2,每个视图的布局权重为1,因此每个视图占据屏幕的1/2