java - 如何在 Android 中以编程方式创建的水平 ScrollView 和 XML 创建的布局?

标签 java android android-layout horizontal-scrolling android-layoutparams

在我的应用程序中,用户可以从图库中选择多个图像,然后他可以在应用程序中查看它。

我在 Relative Layout 中有三个按钮

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.codenemesis.multipleimg.MainActivity">

<Button
    android:id="@+id/button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:onClick="imageSelector"

<Button
    android:id="@+id/button2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:onClick="up"
    android:text="Upload"/>

<Button
    android:id="@+id/button3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:onClick="viewclick"
    android:text="View Images" />

为了显示图像,我在 Horizo​​ntalScrollView 中以编程方式创建的 ImageView 数组设置为 WRAP_CONTENT,但问题是当我单击 View 按钮 Horizo​​ntalScrollView 时横跨整个屏幕,如 this 。 我想在 View 按钮下方显示 Horizo​​ntalScrollView 。我该怎么做?

这是我创建Horizo​​ntalScrollView的方法

  HorizontalScrollView horizontalScrollView = new HorizontalScrollView(this);
                   LinearLayout linearLayout = new LinearLayout(this);
                   ViewGroup.LayoutParams prams = new ViewGroup.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
                   linearLayout.setLayoutParams(prams);
                   linearLayout.setOrientation(LinearLayout.HORIZONTAL);


                   horizontalScrollView.addView(linearLayout);
                   // mArrayUri is the arraylist of images selected from gallery
                   int b = mArrayUri.size();
                   int c = 0;

                   ImageView[] imageViews = new ImageView[mArrayUri.size()];
                   while (c < mArrayUri.size()) {
                       imageViews[c] = new ImageView(this);
                       imageViews[c].setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
                               LinearLayout.LayoutParams.WRAP_CONTENT));
                       imageViews[c].setPadding(10,0,10,0);
                       imageViews[c].setImageURI(mArrayUri.get(c));
                       linearLayout.addView(imageViews[c]);
                       c++;
                       // Set context view
                       setContentView(horizontalScrollView);

最佳答案

你的xml文件中的代码应该是这样的

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.test.myapplication.MainActivity">

<Button
    android:id="@+id/button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="select images"
    android:onClick="imageSelector"/>

<Button
    android:id="@+id/button2"
    android:layout_toRightOf="@+id/button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:onClick="up"
    android:text="Upload"/>

<Button
    android:id="@+id/button3"
    android:layout_toRightOf="@+id/button2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:onClick="viewclick"
    android:text="View Images" />

<RelativeLayout
    android:id="@+id/parent_panel"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/button"
    android:layout_marginTop="10dp"
    >

</RelativeLayout>

java文件中的代码应该是这样的

     public class MainActivity extends AppCompatActivity {
RelativeLayout relativeLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    relativeLayout=findViewById(R.id.parent_panel);
}
public void imageSelector(View view)
{
}
public void up(View view)
{
}
public void viewclick(View view)
{
    HorizontalScrollView horizontalScrollView =
    new HorizontalScrollView(this);
    LinearLayout linearLayout = new LinearLayout(this);
    ViewGroup.LayoutParams prams = new ViewGroup.LayoutParams(
    LinearLayout.LayoutParams.WRAP_CONTENT,
    LinearLayout.LayoutParams.WRAP_CONTENT);
    linearLayout.setLayoutParams(prams);
    linearLayout.setOrientation(LinearLayout.HORIZONTAL);
    horizontalScrollView.addView(linearLayout);
    // mArrayUri is the arraylist of images selected from gallery
    int b = mArrayUri.size();
    int c = 0;
    ImageView[] imageViews = new ImageView[mArrayUri.size()];
    while (c < mArrayUri.size()) {
        imageViews[c] = new ImageView(this);
        imageViews[c].setLayoutParams(new LinearLayout.LayoutParams(
        LinearLayout.LayoutParams.WRAP_CONTENT,
        LinearLayout.LayoutParams.WRAP_CONTENT));
        imageViews[c].setPadding(10, 0, 10, 0);
        imageViews[c].setImageURI(mArrayUri.get(c));
        linearLayout.addView(imageViews[c]);
        c++;
    }
    relativeLayout.addView(horizontalScrollView);
}

}

这应该对你有用。

关于java - 如何在 Android 中以编程方式创建的水平 ScrollView 和 XML 创建的布局?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48480606/

相关文章:

java - 错误 :(136, 81) 错误:不兼容的类型:字符串无法转换为 int

Android:在同一水平线上将一个按钮向左对齐,第二个按钮向右对齐

Android:帮助屏幕向导来指导用户

java - 如何从java中给定的 map 值查找最新日期

java - 启用 URL 重写以在 tomcat 7 上使用 jsessionid

java - WebService 项目 Java on Gradle 项目

java - 如何在 ViewPager 上调用 invalidate()

java - 分组类之间共享的相关常量

java - 在Android中检测 'drag'的开始和结束位置,并在它们之间画一条线

android - 如何禁用 TextInputLayout 上的填充?