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"

この 2 つのタグは相対レイアウト用です。機能させるには、レイアウトの親が相対タイプである必要があります。

android:layout_centerVertical="true"

このタグも正しくありません。textview でテキストを中央に配置する場合は、2 つの方法を使用するか、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 でスキーマを何度も宣言します。ルート レイアウトで 1 回宣言する必要があります。

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

フラグメントが初めてユーザーインターフェイスを描画するときに、システムはこれを呼び出します。フラグメントのUIを描画するには、フラグメントのレイアウトのルートであるこのメソッドからビューを返す必要があります。フラグメントがUIを提供しない場合は、nullを返すことができます。

フラグメントクラスでOnCreateViewを呼び出します

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

フラグメントが表示されないときに、同様の問題がありました。問題は、AndroidAnnotations を削除したことですが、コードを onViewCreated および onCreate メソッドから onCreateView に移動するのを忘れていました。2 つのフラグメントのコードを比較して、この間違いを見つけました。一方は表示され、もう一方は表示されませんでした。したがって、同様の問題がある場合は、フラグメントのコードを確認してみてください。一見するとエラーが表示されない場合がありますが、これらのエラーが原因で機能したり、表示されたりすることはありません。

特に、このメソッドがあり、ビューをインフレートしていることを確認してください。

@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 に答える