简体   繁体   English

如何使图像视图在水平滚动视图内的线性布局中居中?

[英]How to make Image Views centre in a linear layout inside a Horizontal Scroll View?

Example Diagram示例图

I have ImageViews programmatically being added to the linear layout and want these centered in the linear layout, but the layout itself aligned to the left.我以编程方式将 ImageViews 添加到线性布局中,并希望它们在线性布局中居中,但布局本身与左侧对齐。

And in your code, obtain your LinearLayout:在您的代码中,获取您的 LinearLayout:

LinearLayout yourLinearLayout = (LinearLayout) findViewById(R.id.yourLinearLayout);

Define a common LayoutParams object:定义一个通用的LayoutParams object:

  LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
  LinearLayout.LayoutParams.WRAP_CONTENT,
  LinearLayout.LayoutParams.WRAP_CONTENT);

And add your ImageViews to your LinearLayout with a for loop:并使用 for 循环将您的 ImageViews 添加到您的 LinearLayout 中:

for (int i = 0; i < imagesToAdd.size(); i++) {
 ImageView imgView = new ImageView(this);
 imgView.setImageResource("yourResourceID" + i);
 imgView.setLayoutParams(layoutParams);
 yourLinearLayout.addView(imgView);
}

Create a Layout with a HorizontalScrollView as a root view and a LinearLayout as a child创建一个以 Horizo HorizontalScrollView作为根视图并以LinearLayout作为子视图的布局

To center images, make sure to use android:gravity="center" in the LinearLayout要居中图像,请确保在LinearLayout中使用android:gravity="center"

<?xml version="1.0" encoding="utf-8"?>
<HorizontalScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/rootlayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:scrollbars="horizontal">

    <LinearLayout
        android:id="@+id/linearlayout"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:gravity="center"
        android:orientation="horizontal">

    </LinearLayout>

</HorizontalScrollView>

And to add images programmatically, inflate the LinearLayout and use addView() method并以编程方式添加图像,膨胀LinearLayout并使用addView()方法

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        LinearLayout linearLayout = findViewById(R.id.linearlayout);

        linearLayout.addView(getImage(R.drawable.image1));
        linearLayout.addView(getImage(R.drawable.image2));
        linearLayout.addView(getImage(R.drawable.image3));

    }

    ImageView getImage(int drawable) {
        ImageView image = new ImageView(this);
        image.setLayoutParams(new 
                LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, 
                                          ViewGroup.LayoutParams.WRAP_CONTENT));
        image.setImageResource(drawable);
        return image;
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 滚动视图内的水平和垂直线性布局 - Horizontal and Vertical linear layout inside a scroll view 查看在水平线性布局内的动态图像视图中走出屏幕 - View going out of screen in dynamic image views inside horizontal linear layout 如何制作2个图像视图,每个视图的大小可以覆盖水平滚动视图内的线性布局内的屏幕 - How to make 2 images views each be sized to cover screen inside a linear layout inside a horizontal scrollview 水平滚动视图和图像视图? - Horizontal scroll view and image views? 在水平滚动视图中设置线性布局的宽度 - set width of linear layout in horizontal scroll view 如何使水平回收器视图中的水平滚动视图工作? - How to make Horizontal Scroll View inside Horizontal Recycler View work? 如何在水平滚动视图中制作可点击的图像? - How to make clickable image in horizontal scroll view? 如何在Android xml的线性布局中设置滚动视图? - How to set a scroll view inside a Linear Layout in Android xml? 如何在水平线性布局和滚动视图中为其设置切换按钮和文本视图? - How to set a toggle button and text view in horizontal linear layout and scroll view for the same? 在水平视图中看不到相对布局内部的线性布局 - Linear layout inside relative layout is not seen in horizontal view
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM