江の島エンジニアのTips集

android-fragment

(2016/06/30)

android-fragment

src/main/java/com/example/SampleFragment.java

package com.example;

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

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link SampleFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class SampleFragment extends Fragment {

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @return A new instance of fragment TitleLabelFragment.
     */
    public static SampleFragment newInstance() {
        SampleFragment fragment = new SampleFragment();
        Bundle args = new Bundle();
        fragment.setArguments(args);
        return fragment;
    }

    public SampleFragment() {
        // Required empty public constructor
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_title, container, false);
    }
}
<fragment
    android:id="@+id/SampleFragment"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:name="com.example.SampleFragment"
    tools:layout="@layout/fragment_sample"
    />

src/main/res/layout/fragment_sample.xml

<RelativeLayout 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"
    tools:context="com.example.SampleFragment">

    <TextView
        android:id="@+id/TitleLabel"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/TitleLabel" />
    <EditText
        android:id="@+id/TitleInput"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/TitleLabel"
        android:lines="1"
        android:inputType="text"/>

</RelativeLayout>
  • このエントリーをはてなブックマークに追加
  • follow us in feedly