2013-05-09 143 views
1

我有相對佈局,裏面有兩個線性佈局相對佈局誤差線性佈局

該程序沒有第一個線性佈局。任何人都可以解釋爲什麼?

<?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> 
+0

可能是因爲你缺少'機器人:weightSum =在第一個'LinearLayout'中的''',你在其子節點中定義了'android:layout_weight' – jnthnjns 2013-05-09 03:14:37

+1

你得到了什麼錯誤? – jnthnjns 2013-05-09 03:16:00

+0

除了未設置視圖的「高度」或「寬度」以及某些「最佳做法」未解決外,它似乎對我來說工作正常。 – jnthnjns 2013-05-09 03:46:41

回答

0

對於初學者,不要使用@ id/blah,而應該使用@ + id/blah(即使你沒有定義視圖)。它沒有傷害,並且可以防止一些真正難以追查的錯誤。

有關更多詳細信息,請提供您收到的錯誤。

+0

這個答案沒有錯。 這是真的需要一個新的ID。否則,構建器可能會認爲該ID已存在於Ressource文件夾R.java中,或將其鏈接到您不一定想要使用的現有資源 Downvotes應該被證明並編輯到特定級別smh .. 。 – Br0thazS0ul 2013-11-20 22:57:25

3

現在檢查您的佈局。我編輯了一些行。使用@ + 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>