免責事項:この記事はブロガーオリジナル記事です、続くBY-SAのCC 4.0を著作権契約、複製、元のソースのリンクと、この文を添付してください。
フラグメント、一般的にのみ(例えば、以下の広告のような)小さな面積を占めるが、それは(あなたが見にこれらのメソッドを書き換えしようとすることができます)、自身のライフサイクルを持って、次のコードの静的登録と動的登録を使用した例です。
- このようMyFragmentなど、独自のフラグメントを作成します。次のようにレイアウトコードです。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/tv_adv"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:text="广告"
android:textColor="#000000"
android:textSize="17sp"/>
<ImageView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="@+id/iv_adv"
android:layout_weight="5"
android:src="@drawable/adv"
android:scaleType="fitCenter"/>
</LinearLayout>
package xyz.strasae.androidlearn.myandroidapplication;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
public class MyFragment extends Fragment {
protected View view;
protected Context context;
private TextView tv_adv;
private ImageView iv_adv;
@Nullable
@Override
//创建Fragment视图
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
context = this.getActivity();
//从xml文件获取对象 以便代码操作
view = inflater.inflate(R.layout.my_fragment, container, false);
tv_adv = view.findViewById(R.id.tv_adv);
tv_adv.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(context, "你点击了广告的文本", Toast.LENGTH_SHORT).show();
}
});
iv_adv = view.findViewById(R.id.iv_adv);
iv_adv.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(context, "你点击了广告的图片", Toast.LENGTH_SHORT).show();
}
});
return view;
}
}
- 静的登録。直接name属性はフルパスで埋める必要があります、レイアウトファイルを断片化することができるアクティブノードに加え、次のレイアウトファイルとコードの例です。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".StaticFragmentActivity"
android:orientation="vertical"
android:padding="5dp">
<!--每个fragment必须指定id属性 name属性要填写完整的路径-->
<fragment
android:id="@+id/fragment_static_demo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:name="xyz.strasae.androidlearn.myandroidapplication.MyFragment"/>
</LinearLayout>
package xyz.strasae.androidlearn.myandroidapplication;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
public class StaticFragmentActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_static_fragment);
}
}
- 動的登録。ここでは(コードと詳細なコメントですこれは一例に過ぎないことに注意してください、あなたはそれを書く場合はそうのonCreateは毎回(例えば横画面縦画面への変換など)フラグメントを作成しています)。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/ll_dynamic_fragment_layout"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".DynamicFragmentActivity"
android:orientation="vertical">
</LinearLayout>
package xyz.strasae.androidlearn.myandroidapplication;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import android.os.Bundle;
public class DynamicFragmentActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dynamic_fragment);
//获取MyFragment实例
MyFragment myFragment = new MyFragment();
//获取FragmentManager实例
FragmentManager fragmentManager = this.getSupportFragmentManager();
//开启事务
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
//通过事务向布局文件中添加碎片
fragmentTransaction.add(R.id.ll_dynamic_fragment_layout, myFragment);
//提交事务
fragmentTransaction.commit();
}
}