我用horizontalscrollview创建了一个图库。在imageview LinearLayout中我已经放置了一个彩色背景。我希望LinearLayout横跨屏幕的宽度,不管有没有图像。我该怎么办?BackgroundColor在屏幕宽度的LinearLayout中
<LinearLayout
android:id="@+id/linear1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingBottom="5dp"
android:orientation="vertical">
<HorizontalScrollView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:scrollbars="none">
<LinearLayout
android:id="@+id/linear2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:background="@color/estandar"
android:padding="2dp"
/>
</HorizontalScrollView>
</LinearLayout>
main.xml中
LinearLayout linear2 = (LinearLayout) findViewById(R.id.linear2);
LayoutParams params = linear2 .getLayoutParams();
params.height = 100;
params.width = 300;
,但我的代码不能正常工作。
编辑:
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
int anchoPantalla = metrics.widthPixels;
if (listaImagenes.length > 0) {
for (String nombreImagen : listaImagenes) {
InputStream is = getAssets().open(directorioImagenes + "/" + nombreImagen);
final Bitmap bitmap = BitmapFactory.decodeStream(is);
final ImageView imageView = new ImageView(getApplicationContext());
alto = bitmap.getHeight();
ancho = bitmap.getWidth();
final float calculo = ancho/(alto/ALTO_IMAGEN);
imageView.setLayoutParams(new ViewGroup.LayoutParams((int)calculo, ALTO_IMAGEN));
imageView.setImageBitmap(bitmap);
imageView.setPadding(2, 2, 2, 2);
imageView.setBackgroundColor(colorResources);
imageView.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
...
}
});
linear2.addView(imageView);
}
} else {
// PAINT LINE WIDTHSCREEN x 120dp
}
你试图改变你的'ID/linear2'元素的'layout_width'为'fill_parent'? – SparK
是的。 fill_parent y wrap_content执行相同的操作 – user3231711
hmm ...尝试'android:layout_width =“0dp”'和'android:layout_weight =“1”'。如果它的作品,我会发布作为回答 – SparK