【问题标题】:Weight setting for a set of three image in a linear horizontal layout线性水平布局中一组三个图像的权重设置
【发布时间】:2019-05-24 16:36:27
【问题描述】:

我有一个单一的垂直线性布局,里面有一个滚动视图。

我正在以编程方式在其中添加一些东西

  • 一个TextView,没关系:我可以使用它来居中

    LayoutParams params = new LinearLayout.LayoutParams (
    LayoutParams.FILL_PARENT,
    LayoutParams.WRAP_CONTENT
    );
    
    ...
    
    monthNameTextView.setLayoutParams(params);
    monthNameTextView.setGravity(Gravity.CENTER_HORIZONTAL);
    
  • 然后我添加一个水平线性布局。没关系

    gallery = new LinearLayout(this);
    
    gallery.setOrientation(LinearLayout.HORIZONTAL);
    gallery.setGravity(Gravity.CENTER_HORIZONTAL);
    gallery.setLayoutParams(params);
    
  • 然后我添加 3 个从磁盘加载图像的 ImageView

    Bitmap myJpg = BitmapFactory.decodeFile(imgFile.getAbsolutePath());  
    ImageView cover = new ImageView(this);
    cover.setImageBitmap(myJpg);
    gallery.addView(cover);
    

图像已加载,为三张,并以线性布局居中。

问题是一张图片和下一张没有间距。

我是新手,我正在尝试了解 layout_weight 和 weight 的区别,我在这里问您如何以编程方式设置这些参数,以获得一组简单的居中的三个图像,每个图像之间有“一些”间距其中。

【问题讨论】:

  • 您应该考虑为视图之间的一些空间使用“边距”。 “权重”是关于(线性)布局内同级视图的大小分布。
  • 你能解释一下 weight 和 layout-weight 的区别吗?另外:如何以编程方式设置此属性?
  • 我试过 magParams.setMargins(10, 10, 10, 10);但这样一来,顶部有空间,底部有空间,但图像之间没有空间

标签: android android-layout textview android-layout-weight


【解决方案1】:

更改您的代码以将 ImageViews 添加到此:

Bitmap myJpg = BitmapFactory.decodeFile(imgFile.getAbsolutePath());  
ImageView cover = new ImageView(this);
cover.setImageBitmap(myJpg);
LinearLayout.LayoutParams llp = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
llp.setMargins(YOUR_DESIRED_SPACE_VALUE, 0, 0, 0); // 4 margin values for Top/Left/Right/Bottom
gallery.addView(cover, llp);

【讨论】:

  • LayoutParams magParams = new LinearLayout.LayoutParams( LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT ); magParams.setMargins(10, 10, 10, 10); 我刚试过这个,但是这样从顶部和底部都有空间,但图像之间没有空间
  • @realtebo 好的,我编辑了答案,似乎我们应该在将 ImageView 添加到其父级时传递 LayoutParam。
  • 哎呀!我做错了。我将带边框的布局应用于线性布局,而不是应用于单个图像。当应用于图像时,一切正常,谢谢!
【解决方案2】:

是否需要在您的代码中进行布局?如果要显示的图像数量总是三个,只需创建一个 XML 布局文件,然后为它们动态设置图像。

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="horizontal">

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_launcher" 
        android:layout_margin="5dp"/>

    <ImageView
        android:id="@+id/imageView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_launcher" 
        android:layout_margin="5dp"/>

    <ImageView
        android:id="@+id/imageView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_launcher" 
        android:layout_margin="5dp"/>

</LinearLayout>

然后在你的代码中:

ImageView iv1 = (ImageView) findViewById(R.id.imageView1);
iv1.setImageBitmap(YOUR_BITMAP);
// ...

【讨论】:

  • 这是一种我不知道的奇妙方式!如何动态设置此布局?图片每次三张,但标题+3张图片被添加了'n'次。所以从文件中加载布局和简单的设置图片将是一种有趣的方式
猜你喜欢
  • 1970-01-01
  • 2011-08-08
  • 1970-01-01
  • 1970-01-01
  • 2015-07-27
  • 1970-01-01
  • 2019-11-28
  • 2017-08-26
  • 2017-11-20
相关资源
最近更新 更多