0

我有这样的主要布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="horizontal" >
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >
    <Button
        android:id="@+id/btn_showdetails"
        android:layout_width="100dip"
        android:layout_height="wrap_content"
        android:layout_marginLeft="110dip"
        android:layout_marginTop="15dip"
        android:text="Show Details" />

    </LinearLayout>

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="horizontal" 
     android:id="@+id/my_parent_layout"
    >
    </LinearLayout>



</LinearLayout>

我正在尝试使用此类包 com.appnetics 调用 my_parent_layout 中的另一个活动;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.Button;

public class FragmenttestActivity extends FragmentActivity {
    Button LogIn = null ;
    final   FragmentManager fragmentManager =    getSupportFragmentManager()  ;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        LogIn = (Button) findViewById(R.id.btn_showdetails); 
        LogInEvent() ; 
    }
public void LogInEvent(){

        LogIn.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {


                        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
                        details fragment = new details();
                        fragmentTransaction.add(R.id.my_parent_layout, fragment);
                        fragmentTransaction.commit();


            }
        }); 

    }


}

详细布局是:

<?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="match_parent"
    android:orientation="vertical" >

   <TextView
        android:id="@+id/date_time_label"
        android:layout_centerVertical="true"
        android:layout_marginLeft="200dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" 
        android:textSize="32dp"
        android:textStyle="bold"

        android:text="time_label"
        />

    <TextView
        android:id="@+id/last_view_time"
        android:layout_toRightOf="@+id/date_time_label"
        android:layout_alignBaseline="@+id/date_time_label"
        android:layout_marginLeft="8dip"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textSize="32dp"

        />

</LinearLayout>

并且布局代码是 /* $Id: $ */

package com.appnetics;

import java.text.SimpleDateFormat;
import java.util.Date;



import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;




/**
 * DateTime
 */
public class details extends Fragment {
    private static String getDateTimeString(Date time) {
        return new SimpleDateFormat("d MMM yyyy HH:mm:ss")
            .format(time);
    }

    private final String time = getDateTimeString(new Date());

    /** @see android.app.Fragment#onCreateView(android.view.LayoutInflater, android.view.ViewGroup, android.os.Bundle) */
    @Override
    public View onCreateView(
        LayoutInflater inflater,
        ViewGroup container,
        Bundle b)
    {
        View view = inflater.inflate(
            R.layout.details,
            container,
            false);  //!!! this is important

        ((TextView) view.findViewById(R.id.last_view_time))
            .setText(time);

        return view;
    }
}

问题是当点击按钮时,什么都没有出现,知道我的代码有什么问题

4

3 回答 3

6

开始吧!

在您的详细信息 XML 上:

android:layout_toRightOf="@+id/date_time_label"
android:layout_alignBaseline="@+id/date_time_label"

这两个标签用于相对布局,布局的父级必须是相对类型才能工作。

android:layout_centerVertical="true"

这个标签也是不正确的,如果你想在 textview 中居中文本,你可以使用两种方式或在 textView 中包装内容并在其父级中设置中心或使用:

android:gravity="right"
android:width="fill_parent"

这将是 detail_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="match_parent"
android:orientation="vertical" >
<TextView
    android:id="@+id/date_time_label"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" 
    android:textSize="32dp"
    android:textStyle="bold"
    android:text="time_label"
    />

<TextView
    android:id="@+id/last_view_time"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="right"
    android:text="view time"
    android:textSize="32dp" />

</LinearLayout>

在您的 main.xml 中,您多次声明模式!应该在你的根布局上声明一次。

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

所以 main.xml 将是......

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center_horizontal"
android:orientation="vertical" >

<TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/hello" />

<Button
    android:id="@+id/add_fragment"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Button" />

<LinearLayout
    android:id="@+id/fragment_holder"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

</LinearLayout>

</LinearLayout>

片段是在其类中声明并应用其 xml 的元素。

public class DetailsFragment extends Fragment {

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

并在主要活动中将其添加到持有者布局中:

public class FragmentSimpleTestActivity extends FragmentActivity {

private Button addFragment;


/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);   

    final FragmentManager fragmentManager = getSupportFragmentManager();

    addFragment = (Button) findViewById(R.id.add_fragment);

    addFragment.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {   
            FragmentTransaction fragmentTransaction =      fragmentManager.beginTransaction();
            DetailsFragment fragment = new DetailsFragment();
            fragmentTransaction.add(R.id.fragment_holder,fragment, "MY_FRAG");
            fragmentTransaction.commit();
        }
    });
}
}

fragmentTransaction.add 有参数很重要

1:保存片段的视图的ID。2:片段对象。3:用于在堆栈中查找片段的标签。

就这样 :)

享受你的片段!您还可以在添加或删除它时设置动画。

于 2012-05-31T07:27:24.523 回答
0

onCreateView()

当片段第一次绘制其用户界面时,系统会调用它。要为您的 Fragment 绘制 UI,您必须从此方法返回一个 View,它是 Fragment 布局的根。如果片段不提供 UI,您可以返回 null。

在片段类中调用 OnCreateView

 return inflater.inflate(R.layout.fragmentlayout, container, false);
于 2012-05-31T07:15:26.883 回答
0

当片段没有出现时,我遇到了类似的问题。问题是我删除了 AndroidAnnotations,但忘记将代码从 onViewCreated 和 onCreate 方法移动到 onCreateView。我通过比较两个片段的代码发现了这个错误——一个出现了,另一个没有出现。因此,如果您有类似的问题,请尝试检查片段的代码。乍一看,它可能不会显示任何错误,但由于这些错误,它仍然无法工作或显示。

特别是,检查你是否有这个方法并且你膨胀了一个视图:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    View v = inflater.inflate(R.layout.your_fragment_layout, container, false);
    return v;

}
于 2014-09-08T16:36:50.710 回答