我在尝试完全以编程方式制作此布局时遇到问题,每次进程运行结束时都没有成功并且应用程序崩溃。
我试图以编程方式创建的 XML 部分是:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:id="@+id/idImagenPlay0"
android:layout_width="62dp"
android:layout_height="54dp"
android:layout_marginRight="10dp"
android:layout_marginTop="15dp"
android:src="@drawable/ic_action_play" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/idNombre0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="Description"
android:textSize="25sp"
android:textStyle="bold" />
<TextView
android:id="@+id/idInfo0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="15dp"
android:layout_marginRight="10dp"
android:text="Title" />
</LinearLayout>
<ImageView
android:id="@+id/idImagenShare0"
android:layout_width="54dp"
android:layout_height="48dp"
android:layout_alignParentRight="true"
android:layout_marginRight="10dp"
android:layout_marginTop="15dp"
android:src="@drawable/ic_action_share" />
</RelativeLayout>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
我正在尝试使用以下 kotlin 代码重新创建它:
fun createXmlElement(title:String,description:String){
val parent = LinearLayout(this)
parent.layoutParams = LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT)
parent.orientation = LinearLayout.HORIZONTAL
//children of parent linearlayout
val iv = ImageView(this)
val lp = LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, …
Run Code Online (Sandbox Code Playgroud)