「Android/レイアウト/レイアウトファイルをjavaで取得」の版間の差分
提供: 初心者エンジニアの簡易メモ
行7: | 行7: | ||
android:orientation="vertical" > | android:orientation="vertical" > | ||
<LinearLayout | <LinearLayout | ||
− | android:id="@+id/ | + | android:id="@+id/bodyLayout" |
android:layout_width="match_parent" | android:layout_width="match_parent" | ||
android:layout_height="match_parent" | android:layout_height="match_parent" | ||
行34: | 行34: | ||
import android.view.LayoutInflater; | import android.view.LayoutInflater; | ||
− | LinearLayout | + | LinearLayout bodyLayout = findViewById(R.id.bodyLayout); |
LayoutInflater inflater = LayoutInflater.from(this); | LayoutInflater inflater = LayoutInflater.from(this); | ||
LinearLayout itemLayout = (LinearLayout)inflater.inflate(R.layout.video_item, null); | LinearLayout itemLayout = (LinearLayout)inflater.inflate(R.layout.video_item, null); | ||
// id取得 | // id取得 | ||
Button button = (Button)itemLayout.findViewById(R.id.button1); | Button button = (Button)itemLayout.findViewById(R.id.button1); | ||
− | + | bodyLayout.addView(itemLayout); | |
</pre> | </pre> | ||
2019年3月26日 (火) 16:42時点における版
res/layout/main.xml
<LinearLayout 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" android:orientation="vertical" > <LinearLayout android:id="@+id/bodyLayout" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center_horizontal" android:orientation="vertical"> </LinearLayout> </LinearLayout>
res/layout/video_item.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center_horizontal" android:orientation="vertical"> <ImageView android:id="@+id/iconImage" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout>
hogehoge.java
import android.view.LayoutInflater; LinearLayout bodyLayout = findViewById(R.id.bodyLayout); LayoutInflater inflater = LayoutInflater.from(this); LinearLayout itemLayout = (LinearLayout)inflater.inflate(R.layout.video_item, null); // id取得 Button button = (Button)itemLayout.findViewById(R.id.button1); bodyLayout.addView(itemLayout);
参考:http://inujirushi123.blog.fc2.com/blog-entry-27.html
参考:https://developer.android.com/reference/android/view/LayoutInflater