Android LinearLayout始终为全宽

时间:2012-08-14 15:10:35

标签: android xml layout android-linearlayout

我正在尝试为平板电脑设计这样的设计

-640x480区域(左边界)-------灵活的空宽度--------控制区域(右边界) -

灵活的宽度取决于平板电脑尺寸的大小。 Scrollpanes仅用于在移动设备上进行调试。

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:tools="http://schemas.android.com/tools"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/scrollView11"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >

    <HorizontalScrollView
        android:id="@+id/scrollView12"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

        <LinearLayout
            android:id="@+id/ll1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="#24476D" >

            <view
                android:id="@+id/dmi"
                android:layout_width="640dp"
                android:layout_height="480dp"
                android:layout_gravity="left"
                android:layout_marginLeft="10dp"
                android:layout_marginTop="20dp"
                android:layout_weight="1"
                class="com.example.ecorailnet.DMIView"
                android:background="#8fc7ff" />

            <TextView
                android:id="@+id/empty_list_view"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="List view is empty"
                android:visibility="gone" />

            <view
                android:id="@+id/cpp"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="right"
                android:layout_marginLeft="20dp"
                android:layout_marginTop="20dp"
                android:layout_weight="1"
                class="com.example.ecorailnet.ControlPanel" />
        </LinearLayout>
    </HorizontalScrollView>
</ScrollView>

最后的情况现在和它应该是什么样子。

NOW

AFTER

提前致谢

问候

2 个答案:

答案 0 :(得分:1)

如果您只想要左侧的内容以及显示屏右侧的其他内容,请使用RelativeLayout

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#24476D" >

    <view
        android:id="@+id/dmi"
        android:layout_width="640dp"
        android:layout_height="480dp"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
        android:layout_gravity="left"
        android:layout_marginLeft="10dp"
        android:layout_marginTop="20dp"
        class="com.example.ecorailnet.DMIView"
        android:background="#8fc7ff" />

    <view
        android:id="@+id/cpp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:layout_gravity="right"
        android:layout_marginLeft="20dp"
        android:layout_marginTop="20dp"
        class="com.example.ecorailnet.ControlPanel" />

</RelativeLayout>

唯一可以解决的问题是整个东西不适合小屏幕。在这种情况下,两个视图将重叠。

答案 1 :(得分:0)

如果你真的想要一个640x480px DMIView。

您可以尝试覆盖DMIView的onMeasureMethod

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    this.setMeasuredDimension(640,480);
}

或者如果你想要一个占据所有高度的正确缩放DMIView

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    int parentHeight = MeasureSpec.getSize(heightMeasureSpec);
    this.setMeasuredDimension(Math.round(parentHeight * 640 / 480f),parentHeight);      
}

希望这些会有所帮助。