我应该怎么做才能将第4张和第5张图片放在下一行,这是它的外观:
第4和第5张图片走出屏幕如何修复?
MainActivity:
LayoutInflater l = getLayoutInflater();
LinearLayout ll = (LinearLayout) findViewById(R.id.main);
Integer odpowiedzi[] = {R.drawable.kwiaty1, R.drawable.kwiaty2, R.drawable.kwiaty3, R.drawable.kwiaty4, R.drawable.kwiaty5};
for (Integer odp : odpowiedzi) {
View v = l.inflate(R.layout.activ2, null);
ImageView b = (ImageView) v.findViewById(R.id.imageView6);
b.setImageResource(odp);
ll.addView(v);
activ2.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="100dp"
android:layout_height="100dp"
android:id="@+id/imageView6"/>
</LinearLayout>
activity_main.xml中:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/main">
</LinearLayout>
答案 0 :(得分:0)
使线性布局主布局垂直对齐
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/main">
</LinearLayout>
现在可以像这样动态添加布局
获取屏幕宽度
public static int getScreenWidth() {
return Resources.getSystem().getDisplayMetrics().widthPixels;
}
public static float convertDpToPixel(float dp, Context context){
Resources resources = context.getResources();
DisplayMetrics metrics = resources.getDisplayMetrics();
float px = dp * ((float)metrics.densityDpi / DisplayMetrics.DENSITY_DEFAULT);
return px;
}
如果屏幕宽度超过500dp,请在主布局中添加另一个水平对齐的线性布局,并在其中添加imageViews。
否则,如果屏幕宽度小于500dp,请在主布局中添加第一个水平对齐的线性布局,并在其中添加imageViews。然后在主布局中添加另一个水平对齐的线性布局,并在其中添加剩余的imageViews
// Create new LinearLayout
LinearLayout linearLayout = new LinearLayout(this);
linearLayout.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT));
linearLayout.setOrientation(LinearLayout.HORIZONTAL);
// Add textviews
TextView textView1 = new TextView(this);
textView1.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
textView1.setText("programmatically created TextView1");
textView1.setBackgroundColor(0xff66ff66); // hex color 0xAARRGGBB
textView1.setPadding(20, 20, 20, 20); // in pixels (left, top, right, bottom)
linearLayout.addView(textView1);
TextView textView2 = new TextView(this);
LayoutParams layoutParams = new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
layoutParams.gravity = Gravity.RIGHT;
layoutParams.setMargins(10, 10, 10, 10); // (left, top, right, bottom)
textView2.setLayoutParams(layoutParams);
textView2.setText("programmatically created TextView2");
textView2.setTextSize(TypedValue.COMPLEX_UNIT_SP, 18);
textView2.setBackgroundColor(0xffffdbdb); // hex color 0xAARRGGBB
linearLayout.addView(textView2);
// Set context view
mainLinearLayout.addView(linearLayout);
答案 1 :(得分:0)
尝试将activ2上的LinearLayout的宽度更改为0px并将权重更改为1。
在ImageView中添加android:adjustViewBounds="true"
以适合父级。
在activ1中,将match_parent的宽度更改为一些填充。
这样,图像大小将均匀分布,并且应与其父级匹配。
答案 2 :(得分:0)
尝试:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="@+id/imageView6"
android:adjustviewbounds="true"
android:layout_weight="1"/>
</LinearLayout>
您的活动
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/main"
android:weightsum="NUMBER_OF_OBJECTS" >
</LinearLayout>
希望它有所帮助!