Я новичок Android. Могу ли я сделать так: У меня есть XML-файл, который содержит линейную компоновку:Как инициализировать компонент пользовательского интерфейса из файла макета
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/linearLayoutID"
android:orientation="vertical"
android:layout_width="150dp"
android:layout_height="150dp"
android:gravity="center">
<ImageView
android:id="@+id/imageID"
android:contentDescription="@string/nothing"
android:layout_width="80dp"
android:layout_height="80dp"
android:scaleType="centerCrop"
/>
<TextView
android:id="@+id/textID"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:textIsSelectable="false"/>
</LinearLayout>
Из любой части кода, я могу загрузить его в LinearLayout как:
LinearLayout ll = new LinearLayout(R.id.linearLayoutID); ImageView iv = (ImageView) ll.findViewById(R.id.imageID); TextView iv = (TextView) ll.findViewById(R.id.textID);
Помните вопрос, который я хочу спросить это из отдельного файла макета, мы не нагружаем в деятельность или фрагмент, и тем самым использовать findViewByID, мы напрямую загрузить файл XML в компонент пользовательского интерфейса, как LinearLayout, ImageView, ...
Попробуйте сначала, затем спросите –