Как я могу конвертировать XML для программного просмотра в Android Java - PullRequest
0 голосов
/ 20 октября 2019

Я разработал дизайн для секции комментариев в 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>

Все тексты будут отображаться из базы данных. Я знаю, это звучит глупо, но я начинающий, и я действительно не знаю, как это сделать. :) Заранее спасибо!

1 Ответ

0 голосов
/ 20 октября 2019

Используйте один из LayoutInflater методов надувания, который лучше всего подходит для вашего варианта использования. Приведенный ниже пример создаст View, содержащий ваш макет.

View commentView =         LayoutInflater.from(getApplicationContext()).inflate(R.layout.comment_layout,null);
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...