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

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