【问题标题】:BackgroundColor in LinearLayout of the width of the screen屏幕宽度的LinearLayout中的BackgroundColor
【发布时间】:2014-02-15 08:49:46
【问题描述】:

我用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 
        }

【问题讨论】:

  • 您是否尝试过将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


【解决方案1】:
LinearLayout galeria2 = (LinearLayout) findViewById(R.id.linear2);

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

galeria2.setLayoutParams(params);

【讨论】:

  • 只画一条垂直线 1 dp
【解决方案2】:

您必须为 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>

现在在代码中添加类似元素(见高亮行)

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);

【讨论】:

  • 您的代码仅适用于图片以及屏幕宽度。但是我的图像已调整大小。编辑我的代码。
  • 在第二个图像视图 (iv2) 中,我没有设置任何图像。根据您的要求,它只是一个图像视图(我希望 LinearLayout 跨越有或没有图像的屏幕宽度。)。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-06-06
  • 2018-05-30
  • 2012-11-01
  • 2012-08-14
  • 2015-10-10
相关资源
最近更新 更多