[英]How to add layout.xml to another layout.xml programmatically
我有包含具有方向“水平”(ItemViewContainer)的線性布局的片段。 我創建了另一個包含 imageView 和 textView (ItemView) 的布局。
我想以編程方式將 ItemView 添加到 ItemViewContainer 幾次。
我該怎么做而不是從 ItemViewContainer 添加 ItemView。
項目視圖
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="@+id/LLTopServiceRootView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical">
<androidx.cardview.widget.CardView
android:id="@+id/CVServiceIMGHolder"
android:layout_width="50dp"
android:layout_height="50dp"
android:translationZ="5dp"
card_view:cardBackgroundColor="@color/fragment_background_color"
card_view:cardCornerRadius="100dp"
card_view:elevation="5dp">
<ImageView
android:id="@+id/IVServiceIMG"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_gravity="center"
android:tint="@color/text_color"
card_view:srcCompat="@drawable/lights_icon" />
</androidx.cardview.widget.CardView>
<TextView
android:id="@+id/TVServiceName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/text_color"
android:layout_gravity="center_horizontal"
android:text="Service\nName"
android:textAlignment="center" />
</LinearLayout>
項目視圖容器
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:card_view="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="15dp"
android:orientation="vertical"
android:translationZ="5dp"
app:cardBackgroundColor="@color/fragment_background_color"
app:cardCornerRadius="5dp"
card_view:elevation="5dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
android:id="@+id/LLTopServiceContainer"
</LinearLayout>
</androidx.cardview.widget.CardView>
包含 ItemViewContainer 布局的片段:
public class HomeFragment extends Fragment {
private View view;
private TextView mTVPageTitle, mTVHomeNavigationTXT;
private LottieAnimationView mLAVHome;
private ButtonsManager bm;
private LinearLayout mLLTopServiceContainer;
private final String TAG = "HomeLifeCycle";
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragment_home, container, false);
bm = new ButtonsManager(getContext());
initView(view);
Log.d(TAG, "onCreateView: ");
return view;
}
private void initView(View view) {
if (getActivity() != null) {
mLAVHome = getActivity().findViewById(R.id.LAVHome);
mTVPageTitle = getActivity().findViewById(R.id.TVPageTitle);
mTVHomeNavigationTXT = getActivity().findViewById(R.id.TVHome);
mLLTopServiceContainer = view.findViewById(R.id.LLTopServiceContainer);
addSelectedIconStyle();
}
}
}
我想第四次添加 itemView
你可以像這樣動態地做到這一點: Simple Android RecyclerView example
或手動添加:
LayoutInflater inflater = (LayoutInflater)getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LinearLayout linearLayout= (LinearLayout) findViewById(R.id.LLTopServiceContainer);
View view = (View) inflater.inflate(R.layout.item:view_name_file, linearLayout, true);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.