我在XML中创建了一个简单的UI,现在我尝试在Java中实现相同的结果。但由于某种原因,体重对我不起作用。对我来说,在java中创建我的UI非常重要,因为XML不适合我。
这是我的XMl代码:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:weightSum="4">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:weightSum="2">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/french"
android:adjustViewBounds="true"
android:layout_weight="1"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/french"
android:adjustViewBounds="true"
android:layout_weight="1"/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:weightSum="2">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/french"
android:adjustViewBounds="true"
android:layout_weight="1"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/french"
android:adjustViewBounds="true"
android:layout_weight="1"/>
</LinearLayout>
</LinearLayout>
</RelativeLayout>
这里是我的Java代码
RelativeLayout rl = new RelativeLayout(c);
RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.MATCH_PARENT
);
rl.setLayoutParams(rlp);
rl.setBackgroundColor(Color.parseColor("#FFFFFF"));
LinearLayout container = new LinearLayout(c);
LinearLayout.LayoutParams llp = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT
);
container.setLayoutParams(llp);
container.setWeightSum(4f);
container.setOrientation(LinearLayout.VERTICAL);
LinearLayout upperRow = new LinearLayout(c);
llp = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT
);
upperRow.setLayoutParams(llp);
upperRow.setWeightSum(2f);
ImageView ivOne = new ImageView(c);
llp = new LinearLayout.LayoutParams(
0,
LinearLayout.LayoutParams.WRAP_CONTENT
);
llp.weight = 1f;
ivOne.setLayoutParams(llp);
try {
ivOne.setImageDrawable(Drawable.createFromStream(a.getAssets().open("Rest/french.jpg"),null));
} catch (IOException e) {
e.printStackTrace();
}
ImageView ivTwo = new ImageView(c);
llp = new LinearLayout.LayoutParams(
0,
LinearLayout.LayoutParams.WRAP_CONTENT
);
llp.weight = 1f;
ivTwo.setLayoutParams(llp);
try {
ivTwo.setImageDrawable(Drawable.createFromStream(a.getAssets().open("Rest/french.jpg"),null));
} catch (IOException e) {
e.printStackTrace();
}
upperRow.addView(ivOne);
upperRow.addView(ivTwo);
LinearLayout bottomRow = new LinearLayout(c);
llp = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT
);
bottomRow.setLayoutParams(llp);
bottomRow.setWeightSum(2f);
ImageView ivThree = new ImageView(c);
llp = new LinearLayout.LayoutParams(
0,
LinearLayout.LayoutParams.WRAP_CONTENT
);
llp.weight = 1f;
ivThree.setLayoutParams(llp);
try {
ivThree.setImageDrawable(Drawable.createFromStream(a.getAssets().open("Rest/french.jpg"),null));
} catch (IOException e) {
e.printStackTrace();
}
ImageView ivFour = new ImageView(c);
llp = new LinearLayout.LayoutParams(
0,
LinearLayout.LayoutParams.WRAP_CONTENT
);
llp.weight = 1f;
ivFour.setLayoutParams(llp);
try {
ivFour.setImageDrawable(Drawable.createFromStream(a.getAssets().open("Rest/french.jpg"),null));
} catch (IOException e) {
e.printStackTrace();
}
bottomRow.addView(ivThree);
bottomRow.addView(ivFour);
container.addView(upperRow);
container.addView(bottomRow);
rl.addView(container);
a.setContentView(rl);
有人可以向我解释我做错了什么以及如何用Java创建这种UI。
答案 0 :(得分:1)
使用UIKit
代替
LinearLayout.LayoutParams.MATCH_PARENT
表示LinearLayout.LayoutParams.WRAP_CONTENT
和ImageView
包含LinearLayout
s。