2013-04-23 60 views
0

無論我嘗試過,底部的按鈕都看不到。頂部應該有一個TextView,下面有六個按鈕。但不知何故,我看不到他們。我看了here,還有here,但他們沒有幫助。請告訴我,這是什麼問題?這裏是我的佈局:在Android活動中看不到按鈕活動

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_weight="1" > 

    <TextView 
     android:id="@+id/textView1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentTop="true" 
     android:layout_marginLeft="23dp" 
     android:layout_marginTop="17dp" 
     android:text="TextView" /> 

    <Button 
     android:id="@+id/button1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/textView1" 
     android:layout_marginLeft="74dp" 
     android:layout_marginTop="64dp" 
     android:layout_toRightOf="@+id/textView1" 
     android:text="@string/bttn_aux1" 
     android:onClick="is_clicked" /> 

    <Button 
     android:id="@+id/button2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignLeft="@+id/button1" 
     android:layout_below="@+id/button1" 
     android:text="@string/bttn_aux2" 
     android:onClick="can_clicked" /> 

    <Button 
     android:id="@+id/button3" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignLeft="@+id/button2" 
     android:layout_below="@+id/button2" 
     android:text="@string/bttn_aux3" 
     android:onClick="must_clicked" /> 

    <Button 
     android:id="@+id/button4" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignRight="@+id/button3" 
     android:layout_below="@+id/button3" 
     android:text="@string/bttn_aux4" 
     android:onClick="do_clicked" /> 

    <Button 
     android:id="@+id/button5" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignLeft="@+id/button4" 
     android:layout_below="@+id/button4" 
     android:text="@string/bttn_aux5" 
     android:onClick="will_clicked" /> 

    <Button 
     android:id="@+id/button6" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignRight="@+id/button5" 
     android:layout_below="@+id/button5" 
     android:text="@string/bttn_aux6" 
     android:onClick="has_clicked" /> 

</RelativeLayout> 

編輯:另外這是我的活動:

public class AuxilaryVerbs extends Activity{ 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_auxilary); 

     Intent intent = getIntent(); 
     String message = intent.getStringExtra(MainActivity.EXTRAMESSAGE_MAIN); 

     TextView textView = new TextView(this); 
     textView.setTextSize(30); 
     textView.setText(message); 

     setContentView(textView); 
    } 

    public void is_clicked(View view) { 


    } 
    public void can_clicked(View view) { 


    } 
    public void must_clicked(View view) { 


    } 
    public void do_clicked(View view) { 


    } 
    public void will_clicked(View view) { 


    } 
    public void has_clicked(View view) { 


    } 
} 
+0

剛剛嘗試過你的XML,我看到任何問題! – 2013-04-23 23:33:25

+0

真的嗎?我仍然遇到同樣的問題。你認爲這是由於我的虛擬設備設置或什麼?我是新來的;所以我很抱歉我的新手問題。 – 2013-04-23 23:38:15

+0

@MertToka我用正確的問題更新了答案。 – Sharj 2013-04-24 00:21:30

回答

4

Eclipse自動xml生成器增加了很多額外的屬性,你不需要和當你嘗試手動編輯它會迷路。

首先將RelativeLaout替換爲LinearLayout並使其垂直,這將使其更簡單。然後刪除所有layout_toRightOf,left,below等屬性,並使其變得簡單。然後你會看到你所有的按鈕。但是,如果沒有足夠的空間在設備中顯示所有內容,則必須在ScrollView中放置按鈕,以便滾動並獲取所有按鈕。

我嘗試更新XML(可能會有一些錯別字)

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:orientation="vertical" > 

    <TextView 
     android:id="@+id/textView1" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginLeft="23dp" 
     android:layout_marginTop="17dp" 
     android:text="TextView" /> 

    <Button 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginLeft="74dp" 
     android:layout_marginTop="64dp" 
     android:text="@string/bttn_aux1" 
     android:onClick="is_clicked" /> 

    <Button 
     android:id="@+id/button2" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:text="@string/bttn_aux2" 
     android:onClick="can_clicked" /> 

    <Button 
     android:id="@+id/button3" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:text="@string/bttn_aux3" 
     android:onClick="must_clicked" /> 

    <Button 
     android:id="@+id/button4" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:text="@string/bttn_aux4" 
     android:onClick="do_clicked" /> 

    <Button 
     android:id="@+id/button5" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:text="@string/bttn_aux5" 
     android:onClick="will_clicked" /> 

    <Button 
     android:id="@+id/button6" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:text="@string/bttn_aux6" 
     android:onClick="has_clicked" /> 

</LinearLayout> 

編輯 刪除此。你只用這個textView設置你的整個視圖。你已經設定了你的觀點。

setContentView(textView); 

您已使用以下行設置佈局。你不應該再覆蓋它。

setContentView(R.layout.activity_auxilary); 

當您使用佈局文件設置了內容視圖時,您已經擁有了自己的文本視圖。您可以使用以下行訪問它。

TextView textView = (TextView) v.findViewById(R.id.textView1); // id of textview from layoutfile 

就是這樣,不要覆蓋視圖。

+0

我用手機和平板電腦試過這個,我看不到它們。我有可能錯過更基本的東西嗎?我可以在沒有做額外工作的情況下在MainActivity上看到一個按鈕,我是否應該在另一個活動中執行其他操作以查看按鈕? – 2013-04-24 00:08:53

+0

@MertToka像其他人一樣評論說它正在工作。嘗試查看您的活動是否包含正確的layout.xml文件。 – Sharj 2013-04-24 00:17:35

+0

你是對的,問題是因爲s​​etContentView線。但是如果不使用它,我怎麼能在這個活動中看到來自MainActivity的textView?你能推薦我一些東西嗎? – 2013-04-24 00:23:13

0

你以前的參考宣佈ID是不正確的。

<Button 
    android:id="@+id/button1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_below="@+id/textView1" <---no plus sign; "@id/textView1" 
    android:layout_marginLeft="74dp" 
    android:layout_marginTop="64dp" 
    android:layout_toRightOf="@+id/textView1" <---no plus sign; 
    android:text="@string/bttn_aux1" 
    android:onClick="is_clicked" /> 

以下字段有同樣的問題。

僅當您第一次分配ID時才使用加號,以便它將聲明該ID。當id已經被聲明時,不需要使用加號。

+0

感謝您的提示,很高興知道這樣的東西。我使用Graphical Layout創建了界面,所以我認爲Eclipse弄糟了一些東西。但我仍然有這個問題,可能是因爲別的嗎? – 2013-04-23 23:39:34

+0

@MertToka我也沒有問題顯示按鈕了。也許你只是使用屏幕太小的仿真器設備。嘗試在第一個按鈕上刪除'android:layout_marginLeft',看看是否有任何東西出現。 – StoneBird 2013-04-23 23:57:01

+0

什麼是您的設備的屏幕尺寸?或者,也許問題不在佈局中,它在我的.java文件中。我應該做一些特定的活動文件中的按鈕顯示? – 2013-04-24 00:02:44