背景颜色在LinearLayout的屏幕宽度

时间:2014-01-24 11:19:32

标签: android width screen android-linearlayout background-color

我创建了一个带有horizo​​ntalscrollview的画廊。在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 
        }

2 个答案:

答案 0 :(得分:0)

LinearLayout galeria2 = (LinearLayout) findViewById(R.id.linear2);

            LayoutParams params = galeria2.getLayoutParams();
            params.height = 100;
            params.width = 300;

galeria2.setLayoutParams(params);

答案 1 :(得分:0)

您必须为Horizo​​ntalScrollView的每个元素动态添加宽度。我修改了你的代码看看。工作正常。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/linear1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingBottom="5dp"
android:orientation="vertical">

<HorizontalScrollView
    android:id="@+id/hsv"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:scrollbars="none">

    <LinearLayout
            android:id="@+id/linear2"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:orientation="horizontal"
            android:background="#FF0040"
            android:padding="2dp"
      />
</HorizontalScrollView>

现在在代码中添加元素(参见highlited行)

LinearLayout galeria2 = (LinearLayout) findViewById(R.id.linear2);

    ImageView iv1 = new ImageView(this);
    iv1.setImageResource(R.drawable.ic_launcher);
    **iv1.setLayoutParams(new LinearLayout.LayoutParams(
            720, LayoutParams.MATCH_PARENT));**
    ImageView iv2 = new ImageView(this);
    **iv2.setLayoutParams(new LinearLayout.LayoutParams(
            720, LayoutParams.MATCH_PARENT));**

    galeria2.addView(iv1);
    galeria2.addView(iv2);