繁体   English   中英

如何转换 XML 以在 Android Java 中以编程方式查看

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

我为 XML 中的评论部分做了一个设计,但是当我从数据库中获取一些数据时,我想以编程方式创建它。 如何将所有这些转换为 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>

所有文本将从数据库中显示。 我知道这听起来很愚蠢,但我是一个初学者,我真的不知道该怎么做。 :) 先感谢您!

使用最能满足您的用例的LayoutInflater膨胀方法之一。 下面的示例将创建包含您的布局的View

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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