简体   繁体   中英

How to add layout.xml to another layout.xml programmatically

I have Fragment that contain Linear layout with orientation "horizontal" (ItemViewContainer). I'm created another layout that container imageView and textView (ItemView).

I want to add the ItemView to the ItemViewContainer programmatically a few times.

How can i do that instead of adding the ItemView with from 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>

ItemViewContainer

<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>

Fragment that contain layout with the 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();
        }
    }
}

I want to add the itemView 4th times

You could do it dynamically like this way : Simple Android RecyclerView example

Or adding manually :

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM