【问题标题】:linear layout in relative layout error相对布局错误中的线性布局
【发布时间】:2014-06-22 19:48:36
【问题描述】:

我有相对布局,里面有两个线性布局

该程序在没有第一个线性布局的情况下工作。谁能解释一下为什么?

    <?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="@drawable/bngp" >

    <TextView
        android:id="@+id/Cart"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_marginBottom="5dp"
        android:layout_marginTop="5dp"
        android:gravity="center_horizontal"
        android:paddingTop="10dp"
        android:text="Cart"
        android:textColor="#FFFFFF"
        android:textSize="25sp"
        android:textStyle="bold" />

//这是线性布局导致的错误

    <LinearLayout
        android:id="@+id/table"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/Cart"
        android:orientation="horizontal" >

        <TextView
            android:id="@+id/Product"
            android:layout_height="wrap_content"
            android:layout_weight="6"
            android:text="Product"
            android:textColor="#000000" />

        <TextView
            android:id="@+id/Quantity"
            android:layout_height="wrap_content"
            android:layout_weight="3"
            android:gravity="center"
            android:text="Quantity"
            android:textColor="#000000" />

        <TextView
            android:id="@+id/Price"
            android:layout_height="wrap_content"
            android:layout_weight="2"
            android:gravity="center"
            android:text="Price"
            android:textColor="#000000" />

        <TextView
            android:id="@+id/Value"
            android:layout_height="wrap_content"
            android:layout_weight="2"
            android:gravity="center"
            android:text="Value"
            android:textColor="#000000" />
    </LinearLayout>

//线性布局结束


    <ListView
        android:id="@android:id/list"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_above="@+id/textView1"
        android:layout_below="@id/table" />

    <TextView
        android:id="@id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/linear"
        android:paddingBottom="20dp"
        android:paddingTop="20dp"
        android:text="Total Value: "
        android:textColor="#FFFFFF" />

    <TextView
        android:id="@+id/Final_result"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@id/linear"
        android:layout_toRightOf="@id/textView1"
        android:paddingBottom="20dp"
        android:paddingTop="20dp"
        android:textColor="#FFFFFF" />

    <LinearLayout
        android:id="@id/linear"
        style="@android:style/ButtonBar"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true" >

        <Button
            android:id="@+id/button3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Confirm" />

        <Button
            android:id="@+id/button4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Clear" />
    </LinearLayout>

</RelativeLayout>

【问题讨论】:

  • 可能是因为您在第一个 LinearLayout 中缺少 android:weightSum="",所以您在它的孩子中定义了 android:layout_weight
  • 你遇到了什么错误?
  • 除了heightswidths 未设置视图且未解决一些“最佳实践”之外,这对我来说似乎工作正常。

标签: android android-layout


【解决方案1】:

现在检查您的布局。我已经编辑了一些行。使用@+id 代替@id。你必须知道@id、@+id 和@android:id 之间的区别。

即,

"@android:id" which means you are referencing an item in Android namespace.

"@id" means you have defined ids in your application itself, 

例如:-

================================================ =============

<?xml version="1.0" encoding="utf-8"?>
<resources>
  <item name="TextView1" type="id"/>
</resources>

在这种情况下,您已经在资源中定义了一个 textview id。现在你可以使用了,

<TextView
    android:id="@id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
/>

================================================ =============

"@+id" means you are created a view (textview , layouts , etc..) in your layout and you wanted to add the id to R.java.

现在检查你的布局,

<?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="@drawable/bngp" >

<TextView
    android:id="@+id/Cart"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_marginBottom="5dp"
    android:layout_marginTop="5dp"
    android:gravity="center_horizontal"
    android:paddingTop="10dp"
    android:text="Cart"
    android:textColor="#FFFFFF"
    android:textSize="25sp"
    android:textStyle="bold" />

<LinearLayout
    android:id="@+id/table"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/Cart"
    android:orientation="horizontal" >

    <TextView
        android:id="@+id/Product"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="6"
        android:text="Product"
        android:textColor="#000000" />

    <TextView
        android:id="@+id/Quantity"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="3"
        android:gravity="center"
        android:text="Quantity"
        android:textColor="#000000" />

    <TextView
        android:id="@+id/Price"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="2"
        android:gravity="center"
        android:text="Price"
        android:textColor="#000000" />

    <TextView
        android:id="@+id/Value"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="2"
        android:gravity="center"
        android:text="Value"
        android:textColor="#000000" />
</LinearLayout>

<ListView
    android:id="@+id/list"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_above="@+id/textView1"
    android:layout_below="@+id/table" />

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/linear"
    android:paddingBottom="20dp"
    android:paddingTop="20dp"
    android:text="Total Value: "
    android:textColor="#FFFFFF" />

<TextView
    android:id="@+id/Final_result"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/linear"
    android:layout_toRightOf="@+id/textView1"
    android:paddingBottom="20dp"
    android:paddingTop="20dp"
    android:textColor="#FFFFFF" />

<LinearLayout
    android:id="@+id/linear"
    style="@android:style/ButtonBar"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true" >

    <Button
        android:id="@+id/button3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Confirm" />

    <Button
        android:id="@+id/button4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Clear" />
</LinearLayout>

</RelativeLayout>

【讨论】:

    【解决方案2】:

    对于初学者,不要使用@id/blah,而是始终使用@+id/blah(即使您没有定义视图)。它不会造成伤害,并且可以防止一些非常难以追踪的错误。

    有关详细信息,请提供您收到的错误。

    【讨论】:

    • 这个答案没有错。确实,新 ID 需要 +。否则,构建器可能会假设 id 已经存在于 Ressource 文件夹 R.java 中,或者将其链接到您不一定要使用的现有资源 Downvotes 应该被证明并编辑到某个级别 smh...
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-06
    • 2023-03-09
    • 1970-01-01
    • 2019-12-12
    • 2017-10-01
    • 2012-12-17
    相关资源
    最近更新 更多