![](/img/trans.png)
[英]How can I set a view's gravity and layout_gravity programmatically in Java instead of XML?
[英]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.