4

的activity_main.xml中是這樣的Android片段表現怪異

<LinearLayout 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" > 

<Button 
    android:id="@+id/button_one_activity_one" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:text="First Button" 
    /> 

<fragment 
android:name="fragments.FirstFragment" 
android:layout_width="match_parent" 
android:layout_height="wrap_content" 
android:id="@+id/first_fragment" />  

    <Button 
    android:id="@+id/button_two_activity_one" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:text="Second Button" 
    />   
</LinearLayout> 

主要活動類是這樣

package com.example.testfragmentshoneycomb; 

import android.os.Bundle; 
import android.app.Activity; 
import android.view.Menu; 

public class MainActivity extends Activity { 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
} 
} 

first_fragment.xml是這樣

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="wrap_content" 
android:orientation="vertical" 
android:background="@color/grey" >" 

<TextView 
    android:id="@+id/text_view_one_fragment" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:text="Text View one" /> 

<TextView 
    android:id="@+id/text_view_two_fragment" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:text="Text View two" /> 

<TextView 
    android:id="@+id/text_view_three_fragment" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:text="Text View three" /> 

</LinearLayout> 

FirstFragment類是這樣的

package fragments; 


import com.example.testfragmentshoneycomb.R; 

import android.app.Fragment; 
import android.os.Bundle; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 

public class FirstFragment extends Fragment{ 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    // TODO Auto-generated method stub 
    super.onCreate(savedInstanceState); 
} 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) { 
    View view = inflater.inflate(R.layout.first_fragment, container, false); 
    return view; 
} 

} 

它只顯示第一個按鈕,屏幕上沒有其他東西。如果我從activity_main.xml中刪除第一個按鈕,它會顯示片段,但不會顯示第二個按鈕。

敏SDK版本是11和構建目標的是Android 4.1

回答

3

在您的活動佈局中設置了android:orientation="vertical"

+0

非常感謝您 – Atinder

3

它,因爲在默認情況下的LinearLayout的方向是horizontal。因此整個屏幕寬度由First ButtonFragment捕獲。

你確定你想看到它嗎?

First_Button    Fragment   Second_Button 

如果是的話使用layout_weight。如果沒有然後給orientation=vertical到的LinearLayout,它會顯示您的佈局輸出

First_Button    
Fragment 
Second_Button 
+0

非常感謝,不知道默認的行爲 – Atinder

1

集的LinearLayout方向垂直,它的默認水平。 仔細閱讀文檔

1

使用以下佈局:

<LinearLayout 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:orientation="vertical" > 

<Button 
    android:id="@+id/button_one_activity_one" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:text="First Button" 
    /> 

<fragment 
android:name="fragments.FirstFragment" 
android:layout_width="match_parent" 
android:layout_height="wrap_content" 
android:id="@+id/first_fragment" />  

    <Button 
    android:id="@+id/button_two_activity_one" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:text="Second Button" 
    />   
</LinearLayout> 
+0

善意解釋,OP是有問題的。 –

+0

默認情況下,linearlayout採取的方向具有水平。在按鈕視圖中,您已經設置了android:layout_width =「match_parent」,這意味着它將填滿整個寬度並推動片段&第二個按鈕。所以fragment&2nd Button不可見。 – preeya

+0

哎呀!需要添加到您的答案,而不是評論:) –