简体   繁体   中英

How can I convert an XML to make a programmatically view in Android Java

I made a design for a comment section in XML, but I want to create it programmatically when I get some data from database. How can I convert all of these to Java?

<androidx.cardview.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        app:cardCornerRadius="20dp">
       <LinearLayout
           android:layout_width="match_parent"
           android:layout_height="match_parent"
           android:orientation="vertical">

           <LinearLayout
               android:layout_width="match_parent"
               android:layout_height="match_parent"
               android:orientation="horizontal"
               android:weightSum="2">
               <LinearLayout
                   android:layout_width="0dp"
                   android:layout_height="match_parent"
                   android:layout_weight="0.25">

                   <ImageView
                       android:layout_width="50dp"
                       android:layout_height="50dp"
                       android:src="@drawable/complete"
                       android:scaleType="centerCrop"
                       android:layout_gravity="center">

                   </ImageView>
               </LinearLayout>
               <LinearLayout
                   android:layout_width="0dp"
                   android:layout_height="match_parent"
                   android:layout_weight="1.75" >

                   <TextView
                       android:id="@+id/commentFullname"
                       android:layout_width="wrap_content"
                       android:layout_height="wrap_content"
                       android:text="Calin Onaca:"
                       android:textColor="#000"
                       android:layout_gravity="center"/>
                   <TextView
                       android:id="@+id/commentReply"
                       android:layout_width="150dp"
                       android:layout_height="wrap_content"
                       android:layout_gravity="center"
                       android:layout_marginLeft="10dp"
                       android:textColor="#000"
                       android:text="So Bleesed i choosed him no matter what you say i will always choose him"/>

                   <com.google.android.material.button.MaterialButton
                       android:id="@+id/postReply"
                       android:layout_width="wrap_content"
                       android:layout_height="wrap_content"

                       android:text="Reply"
                       android:layout_gravity="center" />
               </LinearLayout>

           </LinearLayout>
           <LinearLayout
               android:layout_width="wrap_content"
               android:layout_height="match_parent"
               android:orientation="horizontal"
               android:layout_marginTop="10dp">
               <TextView
                   android:layout_width="wrap_content"
                   android:layout_height="wrap_content"
                   android:textColor="#000"
                   android:text="Reply: "/>
               <TextView
                   android:id="@+id/replyText"
                   android:layout_width="wrap_content"
                   android:layout_height="wrap_content"
                   android:textColor="#000"
                   android:text="Thank you very much my dear" />
           </LinearLayout>
       </LinearLayout>

    </androidx.cardview.widget.CardView>

All the texts will be displayed from database. I know it sounds stupid, but I'm a begginer and I really don't know how to do this. :) Thank you in advance!

Use one of the LayoutInflater inflate methods that best satisfies your use case. The below example will create View containing your layout.

View commentView =         LayoutInflater.from(getApplicationContext()).inflate(R.layout.comment_layout,null);

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