LoginSignup
0
0

More than 1 year has passed since last update.

fragment勉強記録1

Posted at

 静的にFragmentを呼び出す

fragmentがまじでわけわからんのでmainActivity(緑)の中にfragment(青)を呼び出して表示させることから試してみた。
image.png

mainActivity.java

mainActivity.java でやることは activity_main.xmlを表示するだけ

mainActivity.java

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
}

activity_main.xml

  • main activity からsetContentViewで呼び出すレイアウト
  • ここではconstraint layout で作成(どのレイアウトを使っても大丈夫)
  • fragmentタグを使って中央にfragmentを配置
  • ポイントはandroid:name 属性とtools:layout属性
    • android:name属性では書いてあるクラスをインスタンス化する。つまり、この場合ではsampleAppプロジェクトのsampleFragmentクラスをインスタンス化する。sampleFragmentクラスではフラグメントのレイアウトを生成するので、フラグメントが表示されるようになる。
    • tools:layout属性では、レイアウトプレビューでフラグメント内に描画されるレイアウトを宣言できる。これが無くてもコードは実行されてアプリ内でフラグメントは表示されるが、あるとプレビューの中で描画されるのでレイアウトの設定がやりやすい。
activity_main.xml

<androidx.constraintlayout.widget.ConstraintLayout 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"
    android:background="#8BC34A"
    tools:context=".MainActivity">

    <fragment
        android:id="@+id/fragment"

        android:name="com.example.sampleapp.SampleFragment"
        tools:layout="@layout/fragment_sample"

        android:layout_width="match_parent"
        android:layout_height="300dp"
        android:layout_marginStart="16dp"
        android:layout_marginTop="100dp"
        android:layout_marginEnd="16dp"
        android:layout_marginBottom="100dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
         />

</androidx.constraintlayout.widget.ConstraintLayout>

参考

↓tools:layout属性ありの時
image.png

↓tools:layout属性なしの時
image.png

SampleFragment.java

OnCreateView()で渡されたlayout inflaterにfragment_sample.xmlのレイアウトを挿入して返す。

SampleFragment.java

public class SampleFragment extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        //引数で渡されるLayoutInflaterにFragmentのレイアウトをinflate(挿入)して返す
        return inflater.inflate(R.layout.fragment_sample, container, false);
    }
}

fragment_sample.xml

constraintLayout(青色)の中にtextViewを配置。

fragment_sample.xml
<androidx.constraintlayout.widget.ConstraintLayout 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"
    android:background="#00BCD4"
    tools:context=".SampleFragment">

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_fragment"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

↓fragment
image.png

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0