【问题标题】:Android Fragment behaving weirdAndroid Fragment 行为怪异
【发布时间】:2012-12-28 09:46:59
【问题描述】:

activity_main.xml是这样的

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

主要的activity类是这样的

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

【问题讨论】:

    标签: android android-layout android-fragments fragment android-3.0-honeycomb


    【解决方案1】:

    在您的活动布局中设置android:orientation="vertical"

    【讨论】:

      【解决方案2】:

      因为默认 LinearLayout 的方向是horizontal。因此,整个屏幕宽度由First ButtonFragment 捕获。

      你确定要看看吗?

      First_Button              Fragment            Second_Button
      

      如果是,请使用layout_weight。如果否,则将 orientation=vertical 提供给 LinearLayout,它将显示您的布局输出为

      First_Button              
      Fragment
      Second_Button
      

      【讨论】:

      • 非常感谢,不知道默认行为
      【解决方案3】:

      将 LinearLayout 方向设置为垂直,默认为水平。 仔细阅读文档

      【讨论】:

        【解决方案4】:

        使用以下布局:

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

        【讨论】:

        • 请解释一下 OP 遇到的问题。
        • 默认线性布局采用水平方向。在 Button View 中,您设置了 android:layout_width="match_parent" 这意味着它将填充整个宽度,推动片段和 2nd Button 。所以片段和第二个按钮不可见。
        • 哎呀!需要添加到您的答案中而不是评论:)
        猜你喜欢
        • 2013-05-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-10-14
        • 1970-01-01
        • 2019-11-03
        相关资源
        最近更新 更多