FragmentでfindViewByIdを使用するにはどうすればよいですか?
この例は、フラグメントでfindViewByIdを使用する方法について示しています
ステップ1 − Android Studioで新しいプロジェクトを作成し、[ファイル]⇒[新しいプロジェクト]に移動して、新しいプロジェクトを作成するために必要なすべての詳細を入力します。
ステップ2 −次のコードをres / layout/activity_main.xmlに追加します。
<?xml version = "1.0" encoding = "utf-8"?> <LinearLayout xmlns:android = "https://schemas.android.com/apk/res/android" android:orientation = "vertical" android:layout_width = "match_parent" android:layout_height = "match_parent"> <LinearLayout android:id = "@+id/linearlayout01" android:layout_width = "fill_parent" android:layout_height = "fill_parent" android:background = "#ccc" android:layout_weight = "1" android:orientation = "vertical"> <fragment android:name = "com.example.myapplication.FirstFragment" android:id = "@+id/frag_1" android:layout_width = "fill_parent" android:layout_height = "fill_parent" /> </LinearLayout> <LinearLayout android:id = "@+id/linearlayout02" android:layout_width = "fill_parent" android:layout_height = "fill_parent" android:layout_weight = "1" android:background = "#eee" android:orientation = "vertical"> <fragment android:name = "com.example.myapplication.SecondFragment" android:id = "@+id/frag_2" android:layout_width = "fill_parent" android:layout_height = "fill_parent" /> </LinearLayout> </LinearLayout>
上記のコードでは、2つのフラグメントを取得しています。
ステップ3 −次のコードをsrc / MainActivity.java
に追加します<?xml version = "1.0" encoding = "utf-8"?>
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
public class MainActivity extends FragmentActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
} ステップ4 −次のコードをsrc / FirstFragment.java
に追加します<?xml version = "1.0" encoding = "utf-8"?>
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;
public class FirstFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
ViewGroup root = (ViewGroup) inflater.inflate(R.layout.fragment, null);
TextView but = (TextView) root.findViewById(R.id.text);
return root;
}
} ステップ4 −次のコードをsrc / SecondFragment.java
に追加します<?xml version = "1.0" encoding = "utf-8"?>
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class SecondFragment extends Fragment {
TextView textView;
View view;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragment, container, false);
return view;
}
} アプリケーションを実行してみましょう。実際のAndroidモバイルデバイスをコンピューターに接続していると思います。 android studioからアプリを実行するには、プロジェクトのアクティビティファイルの1つを開き、ツールバーの[実行]アイコンをクリックします。オプションとしてモバイルデバイスを選択し、デフォルトの画面を表示するモバイルデバイスを確認します–
-
Androidでスナックバーを使用する方法は?
この例は、AndroidでsnackBarを使用する方法を示しています。 ステップ1 − Android Studioで新しいプロジェクトを作成し、[ファイル]⇒[新しいプロジェクト]に移動して、新しいプロジェクトを作成するために必要なすべての詳細を入力します。 ステップ2 −次のコードをres / layout/activity_main.xmlに追加します。 <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="https://sche
-
AndroidでNavigationViewを使用する方法は?
この例は、AndroidでNavigationViewを使用する方法を示しています。 ステップ1 − Android Studioで新しいプロジェクトを作成し、[ファイル]⇒[新しいプロジェクト]に移動して、新しいプロジェクトを作成するために必要なすべての詳細を入力します。 ステップ2 −次のコードをres / layout/activity_main.xmlに追加します。 <?xml version="1.0" encoding="utf-8"?> <androidx.drawerlayout.widget.DrawerLayou