如何使用eclipse为xml代码编写动态代码 适用于Android的IDE。由于我是初学者,我不知道写动态代码PLZ帮助我.. .................................................. .................................................. .................................................. ................................
<TableLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" >
<TableRow
android:id="@+id/tableRow1"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="150dp"
android:layout_height="200dp"
android:layout_marginRight="5dp"
android:scaleType="fitXY"
android:layout_weight="1"
android:src="@drawable/image_new1" />
<ImageView
android:id="@+id/imageView2"
android:layout_width="150dp"
android:layout_height="fill_parent"
android:layout_marginRight="5dp"
android:scaleType="fitXY"
android:layout_weight="1"
android:src="@drawable/image_new2" />
<ImageView
android:id="@+id/imageView3"
android:layout_width="150dp"
android:layout_height="fill_parent"
android:layout_marginRight="5dp"
android:scaleType="fitXY"
android:layout_weight="1"
android:src="@drawable/image_new2" />
<ImageView
android:id="@+id/imageView4"
android:layout_width="150dp"
android:layout_height="fill_parent"
android:layout_marginRight="5dp"
android:scaleType="fitXY"
adroid:layout_weight="1"
android:src="@drawable/image_new1" />
</TableRow>
</TableLayout>
答案 0 :(得分:0)
这是一个动态代码,但是使用LinearLayout并在这种情况下添加你的项目view_list_item实际上你也可以这样做。
在你的活动布局中放了这个。
<LinearLayout
android:id="@+id/view_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/splitter"
android:layout_marginTop="10dp"
android:orientation="vertical" >
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<!-- as they are added to and removed from the LinearLayout. -->
<LinearLayout
android:id="@+id/container_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:animateLayoutChanges="true"
android:divider="?android:dividerHorizontal"
android:orientation="vertical"
android:showDividers="middle" />
</ScrollView>
</LinearLayout>
//还为view_list_item创建另一个布局并根据需要进行更改
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/main"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<com.csdc.crsmofa.views.TextViewNaskhRegular
android:id="@+id/item_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="3dp"
android:layout_alignParentRight="true"
android:textAppearance="@style/LoginTextViewAppearnce" />
<com.csdc.crsmofa.views.TextViewNaskhRegular
android:id="@+id/item_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toLeftOf="@+id/item_label"
android:lines="1" />
</RelativeLayout>
//在您的活动中
onCreate(...){
ViewGroup containerView = (ViewGroup) view.findViewById(R.id.container_view);
fillView(containerView);
}
private void fillView(final ViewGroup mContainerView) {
try {
mContainerView.removeAllViews();
ViewGroup newView = (ViewGroup) LayoutInflater
.from(getActivity())
.inflate(R.layout.view_list_item, mContainerView, false);
((TextView) newView.findViewById(R.id.item_label))
.setText("blablabla");
((TextView) newView.findViewById(R.id.item_text))
.setText("blablabla");
mContainerView.addView(newView, 0);
} catch (Exception ex) {
String string = ex != null ? ex.getMessage() : "";
Log.e(TAG, string != null ? string : "");
ex.printStackTrace();
}
}