简体   繁体   English

LinearLayout里面的RelativeLayout?

[英]RelativeLayout inside LinearLayout?

I was wondering what I am doing wrong here, why does surrounding these buttons in a RelativeLayout cause an error? 我想知道我在这里做错了什么,为什么在RelativeLayout中包围这些按钮会导致错误? I haven't edited layouts much, can I not just stick in a Relative or Linear layout like that? 我没有很多编辑布局,我不能只是坚持相对或线性布局吗?

If I don't surround the buttons in a layout they appear beneath each other, I am just trying to experiment in having them beside each other horizontally. 如果我没有将布局中的按钮环绕在一起,它们就会出现在彼此之下,我只是想尝试将它们水平放在彼此旁边。

I think the way I have done this whole layout is bad because vertically it looks quite squished. 我认为我完成整个布局的方式很糟糕,因为垂直方向看起来非常紧张。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/MainLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >


   <LinearLayout android:id="@+id/topButtons" 
    android:layout_margin="4dip"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">
    <Button android:id="@+id/deviceConnect"
        android:layout_margin="8dip"
        android:layout_weight="3"
        android:layout_width="fill_parent"
        android:layout_height="match_parent"
        android:text="Connect2"/>
    <LinearLayout android:orientation="vertical"
        android:layout_weight="1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">
        <LinearLayout android:orientation="horizontal"
            android:layout_margin="8dip"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">
            <TextView android:text="Connected Adapter:"
                android:layout_gravity="center_vertical"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"/>
            <Spinner android:id="@+id/deviceSpinner"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:drawSelectorOnTop="true"/>
            <TextView android:id="@+id/currentSettings" 
                android:layout_marginLeft="8dip"
                android:text="Current Settings: Not Connected"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_vertical"/>
        </LinearLayout>
        <LinearLayout android:orientation="horizontal"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content">
            <LinearLayout android:orientation="horizontal"
                android:layout_margin="8dip"
                android:layout_weight="1"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content">
                <TextView android:text="Baud:"
                    android:layout_gravity="center_vertical"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"/>
                <Spinner android:id="@+id/baudSpinner"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:drawSelectorOnTop="true"/>
            </LinearLayout>
            <LinearLayout android:orientation="horizontal"
                android:layout_margin="8dip"
                android:layout_weight="1"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content">
                <TextView android:text="Data:"
                    android:layout_gravity="center_vertical"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"/>
                <Spinner android:id="@+id/dataSpinner"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:drawSelectorOnTop="true"/>
            </LinearLayout>
            <LinearLayout android:orientation="horizontal"
                android:layout_margin="8dip"
                android:layout_weight="1"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content">
                <TextView android:text="Parity:"
                    android:layout_gravity="center_vertical"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"/>
                <Spinner android:id="@+id/paritySpinner"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:drawSelectorOnTop="true"/>
            </LinearLayout>
            <LinearLayout android:orientation="horizontal"
                android:layout_margin="8dip"
                android:layout_weight="1"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content">
                <TextView android:text="Stop:"
                    android:layout_gravity="center_vertical"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"/>
                <Spinner android:id="@+id/stopSpinner"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:drawSelectorOnTop="true"/>
            </LinearLayout>
        </LinearLayout>
    </LinearLayout>
    <Button android:id="@+id/updateSettings"
        android:layout_margin="8dip"
        android:layout_weight="3"
        android:layout_width="fill_parent"
        android:layout_height="match_parent"
        android:text="Update\nSettings"/>
</LinearLayout>
<RelativeLayout>
   <Button android:id="@+id/Command"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Test Command"/> 

   <Button android:id="@+id/Command"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Test Command"/> 

   <Button android:id="@+id/Command"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Test Command"/> 

   <Button android:id="@+id/Command"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Test Command"/> 

   <Button android:id="@+id/Command"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Test Command"/> 
    </RelativeLayout>

<jackpal.androidterm.emulatorview.EmulatorView
    android:id="@+id/emulatorView"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:focusable="true"
    android:focusableInTouchMode="true"

    />

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_weight="0"
    android:orientation="horizontal" >

    <EditText
        android:id="@+id/term_entry"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:singleLine="true"
        android:imeOptions="actionNone|flagNoExtractUi"
        android:inputType="text|textImeMultiLine" />

    <Button
        android:id="@+id/term_entry_send"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="0"
        android:text="@string/entry_send" />

</LinearLayout>

As others have pointed out already, you need to define a layout_width and layout_height for your RelativeLayout . 正如其他人已经指出的那样,您需要为RelativeLayout定义layout_widthlayout_height This should fix your error, unless there are other issues you have. 这应该可以解决您的错误,除非您有其他问题。 But I thought it was worth noting what the DOCS say about layouts 但我认为值得注意的是DOCS对布局的看法

A RelativeLayout is a very powerful utility for designing a user interface because it can eliminate nested view groups and keep your layout hierarchy flat, which improves performance. RelativeLayout是一个非常强大的实用程序,用于设计用户界面,因为它可以消除嵌套视图组并保持布局层次结构平整,从而提高性能。 If you find yourself using several nested LinearLayout groups, you may be able to replace them with a single RelativeLayout. 如果您发现自己使用了多个嵌套的LinearLayout组,则可以使用单个RelativeLayout替换它们。

You have many nested LinearLayout s so you may consider using a RelativeLayout for better performance and readability. 您有许多嵌套的LinearLayout因此您可以考虑使用RelativeLayout以获得更好的性能和可读性。 You can also use android:orientation="horizontal" in a nested LinearLayout to get your buttons next to each other. 您还可以在嵌套的LinearLayout使用android:orientation="horizontal"来使您的按钮彼此相邻。 But, again, a RelativeLayout may be better in your situation instead of having all of the nested layouts. 但是,在您的情况下, RelativeLayout可能会更好,而不是拥有所有嵌套布局。

Your relativeLayout has no size. 你的relativeLayout没有大小。

Define layout_height and layout_width to it 为其定义layout_height和layout_width

        <RelativeLayout 
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM