右侧 View 的 Android 布局占用预设空间,左侧 View 会缩放以填充屏幕的其余部分,直至达到一定限度

标签 android android-layout android-xml android-screen android-screen-support

我正在 Android 中制作一个布局(XML 文件),它有两个 subview (一个在屏幕右侧,一个在屏幕左侧)。我希望右边的 View 占用一个预先确定的空间(以 dp 为单位),并让左边的 View 占用所有剩余空间达到一个限制,此时它将停止扩展,两个布局将只随着屏幕变大,移动得更远。

奇怪的是,如果我希望右侧的 View 是展开的 View ,而左侧的 View 是占据预设空间的 View ,这将非常容易。如果您将每个 View 设置为您想要的宽度(在水平线性布局中),Android 将在两个 View 不适合的情况下自动缩小左侧的 View 。

我想在一个布局文件中做到这一点;此布局已设计用于 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: 我使用背景色只是为了清楚地显示布局。完全没有必要。 :)

编辑 2:如果您想要一种将左侧布局扩展到限制的方法,请试试这个 -

<?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">
        <LinearLayout 
                            android:id="@+id/inner"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"         
            android:background="@color/yellow">

            <TextView
                android:id="@+id/myTextView"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="This will expand only to a limit"
                android:maxWidth="300dip"
                android:textAppearance="?android:attr/textAppearanceLarge" 
                android:background="@color/red" />

        </LinearLayout>
    </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>

在这里您需要将小部件放置在内部布局中,这就是它在不同屏幕中的样子。

3.1 英寸 HVGA 3.2in HVGA

5.2 英寸 QVGA 5.2in QVGA

使用的颜色:红色(#FFFF00)、黄色(#FFFFFF00)、绿色(#FF00FF00)。

关于右侧 View 的 Android 布局占用预设空间,左侧 View 会缩放以填充屏幕的其余部分,直至达到一定限度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13206973/

相关文章:

android - 在纵向模式android中防止滚动

android - 为标签 LinearLayout 找到了意外的命名空间前缀 "xmlns"

java - onActivityResult()之后动态添加的imageView消失

android - GridView 只显示一行

android - 通过在 xml 中设计来制作自定义组件

Android - 如何对齐 editText 和提交按钮底部?

android - 前台服务 onTaskRemoved 发送广播 Intent 失败 android.os.DeadObjectException

android - 如何从 OpenCV 返回图像并在 Android Java 代码中显示它?

android - 如何更快地加载 android google map 标记?

android - 从 Android 中的驱动程序获取 HCI 设备列表的问题