2012-11-03 41 views
0

所以我一直在努力弄清楚佈局和使用佈局inflater,但我遇到了一些問題。我有兩個相對的XML佈局文件,一個XML文件有兩個textViews,一個EditText字段和微調對象:爲什麼我的兩個視圖都不顯示?

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/firstLayout" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" > 

<TextView 
    android:id="@+id/goalNameView" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentTop="true" 
    android:layout_marginLeft="5dip" 
    android:text="@string/goalName" /> 

<EditText 
    android:id="@+id/goal_name" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_alignParentTop="true" 
    android:layout_toRightOf="@+id/goalNameView" 
    android:hint="@string/editText" 
    android:ems="10" > 

    <requestFocus /> 
</EditText> 

<TextView 
    android:id="@+id/goalTasksView" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignBaseline="@+id/goal_tasks" 
    android:layout_alignBottom="@+id/goal_tasks" 
    android:layout_alignLeft="@+id/goalNameView" 
    android:text="@string/goalTasks" /> 

<Spinner 
    android:id="@+id/goal_tasks" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_toRightOf="@+id/goalTasksView" 
    android:layout_marginTop="51dp" /> 


</RelativeLayout> 

其它XML文件有一個TextView的和一個EDITTEXT場:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:id="@+id/tView" 
> 
<EditText 
    android:id="@+id/taskNameField" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_toRightOf="@+id/taskNameView" 
    android:ems="10" 
    android:hint="@string/editText" /> 

<TextView 
    android:id="@+id/taskNameView" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignBaseline="@+id/taskNameField" 
    android:layout_alignBottom="@+id/taskNameField" 
    android:layout_alignParentLeft="true" 
    android:text="@string/taskName" 
    /> 

</RelativeLayout> 

我想兩個佈局與第三xml文件(main.xml中)拼湊:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 

android:layout_width="wrap_content" 
android:layout_height="match_parent" 
android:orientation="vertical" 
android:id="@+id/theMain" 
> 

<include android:id="@id/mainLayout" layout="@layout/activity_goal_keeper" /> 



</LinearLayout> 

我期待有佈置工作的方法是顯示的第一個佈局(@ ID/firstLayout)一個然後直接在第二個佈局(@ id/tView)之下。我讀過我需要在LinearLayout上實現佈局來實現這一點,這就是爲什麼我有第三個佈局(@ id/theMain)。

正如你所看到的,我有一個包含@ id/firstLayout的聲明,但是我沒有@ id/tView的包含語句,因爲我試圖通過我的main來膨脹@ id/tView的多個版本活動:

public class GoalKeeperActivity extends Activity implements  AdapterView.OnItemSelectedListener { 
public Integer[] items= {1,2,3,4,5,6,7,8}; 

@Override 
public void onCreate(Bundle icicle) { 
    super.onCreate(icicle); 
    //setContentView(R.layout.activity_goal_keeper); 
    setContentView(R.layout.main); 

    Spinner numOfTasks=(Spinner) findViewById(R.id.goal_tasks); 
    numOfTasks.setOnItemSelectedListener(this); 

    ArrayAdapter<Integer> aa = new ArrayAdapter<Integer>(this, android.R.layout.simple_spinner_item, items); 

    aa.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
    numOfTasks.setAdapter(aa); 

    ViewGroup item = (ViewGroup) findViewById(R.id.theMain); 

    for(int i=0; i<3;i++) 
    { 
     View child = getLayoutInflater().inflate(R.layout.tasks_view, item, false); 
     child.setId(i); 
     item.addView(child); 

    } 

我的問題是@firstLayout顯示正常,但@tView根本不顯示。有人能解釋我錯過了什麼嗎?

在此先感謝。

+0

可能它與你打開的'TextView'標籤有關嗎? (這是你的帖子上面全黑的那個) – Eric

+0

@Eric - 謝謝,我一定把這個意外刪除了,這只是一個錯字。我將編輯原始帖子 – Kyle

回答

0

我想通了,這只是一個簡單的問題,在@ id/tView xml文件中設置「android:layout_height」屬性等於wrap_content。愚蠢的小錯誤...

相關問題