【发布时间】:2014-02-15 08:49:46
【问题描述】:
我用horizontalscrollview 创建了一个画廊。在 imageview LinearLayout 我放了一个彩色背景。我希望 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? -
是的。 fill_parent y wrap_content 做同样的事情
-
嗯...试试
android:layout_width="0dp"和android:layout_weight="1"。如果可行,我将作为答案发布
标签: android width screen android-linearlayout background-color