我在
Android中制作了一个布局(XML文件),它有两个子视图(一个位于屏幕右侧,另一个位于左侧).我希望右边的视图占据一个预定的空间(在dp中)并让左边的视图占用所有剩余的空间达到极限,此时它将停止扩展,这两个布局将只是随着屏幕变大,进一步分开.
奇怪的是,如果我希望右侧的视图是扩展的视图,并且左侧的视图是占用预设空间的视图,这将非常容易.如果您将每个视图设置为所需的宽度(在水平线性布局中),如果两个视图都不适合,Android将自动缩小左侧的视图.
我想在一个布局文件中执行此操作;这种布局已经设计用于sw512dp-land和sw765dp-land之间的显示.
如果我能找到一种方法让Android缩小左侧的布局(当布局都不能适合指定的大小时),下面的代码就可以工作了.但默认情况下,系统会先缩小右侧的布局.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="horizontal" > <RelativeLayout android:id="@+id/left" android:layout_width="150dip" android:layout_height="fill_parent" android:background="@color/red" > </RelativeLayout> <LinearLayout android:id="@+id/right" android:layout_width="100dip" android:layout_height="fill_parent" android:layout_alignParentRight="true" android:layout_alignParentTop="true" android:background="@color/green" > </LinearLayout>
如果我不需要左边的布局来停止某个点的扩展,那么这个代码(来自@Lokesh)就可以工作了.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="horizontal" > <LinearLayout android:id="@+id/left" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_toLeftOf="@+id/right" android:background="@color/red" > </LinearLayout> <LinearLayout android:id="@+id/right" android:layout_width="100dip" android:layout_height="fill_parent" android:layout_alignParentRight="true" android:layout_alignParentTop="true" android:background="@color/green" > </LinearLayout>
如果有人认为这是不可能的,那么我也可以采取实际操作或改变我的布局方法.
谢谢!
解决方法
这适合我.我已将正确的布局设置为100dip,根据您的需要进行更改.
<?xml version="1.0" encoding="utf-8" ?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="horizontal" > <LinearLayout android:id="@+id/left" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_toLeftOf="@+id/right" android:background="@color/red" > </LinearLayout> <LinearLayout android:id="@+id/right" android:layout_width="100dip" android:layout_height="fill_parent" android:layout_alignParentRight="true" android:layout_alignParentTop="true" android:background="@color/green" > </LinearLayout> </RelativeLayout>
编辑1:我使用背景颜色只是为了清楚地显示布局.根本没必要.