Android:如何让代码创建的HorizontalScrollView显示在XML布局按钮下方?
Got it, let's sort out this issue for you. The core problem here is that setContentView(horizontalScrollView) replaces your entire original RelativeLayout with the scroll view—so it naturally takes over the whole screen. Instead, we need to add the HorizontalScrollView to your existing RelativeLayout and explicitly position it below the "View Images" button using layout rules.
Step 1: Add an ID to your root RelativeLayout
First, update your XML layout to give the root RelativeLayout an ID (so we can reference it in code):
<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:id="@+id/root_layout" <!-- Add this ID --> 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" /> </RelativeLayout>
Step 2: Modify your viewclick method
Instead of replacing the entire layout, we'll create the scroll view, set its position to sit below button3, then add it to the root RelativeLayout. We'll also handle duplicate clicks to avoid adding multiple scroll views:
public void viewclick(View view) { // Get reference to the root RelativeLayout from your XML RelativeLayout rootLayout = findViewById(R.id.root_layout); // Remove any existing scroll view first to avoid duplicate views on repeated clicks View existingScrollView = rootLayout.findViewById(R.id.image_scroll_view); if (existingScrollView != null) { rootLayout.removeView(existingScrollView); } // Create HorizontalScrollView with layout params to position it below button3 HorizontalScrollView horizontalScrollView = new HorizontalScrollView(this); RelativeLayout.LayoutParams scrollParams = new RelativeLayout.LayoutParams( RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT ); scrollParams.addRule(RelativeLayout.BELOW, R.id.button3); // Critical: place below the View Images button horizontalScrollView.setLayoutParams(scrollParams); horizontalScrollView.setId(R.id.image_scroll_view); // Set ID to track it later // Create inner LinearLayout for holding images LinearLayout linearLayout = new LinearLayout(this); LinearLayout.LayoutParams linearParams = new LinearLayout.LayoutParams( LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT ); linearLayout.setLayoutParams(linearParams); linearLayout.setOrientation(LinearLayout.HORIZONTAL); horizontalScrollView.addView(linearLayout); // Add your selected images to the LinearLayout for (int c = 0; c < mArrayUri.size(); c++) { ImageView imageView = new ImageView(this); LinearLayout.LayoutParams imgParams = new LinearLayout.LayoutParams( LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT ); imageView.setLayoutParams(imgParams); imageView.setPadding(10, 0, 10, 0); imageView.setImageURI(mArrayUri.get(c)); linearLayout.addView(imageView); } // Finally, add the scroll view to your existing root layout rootLayout.addView(horizontalScrollView); }
Step 3: Ensure you load the original layout first
Double-check your onCreate method loads the XML layout initially (don't skip this step!):
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // Load your original RelativeLayout first }
Why this works:
- We're not replacing the entire screen layout anymore—we're adding the scroll view as a child to your existing
RelativeLayout. - The
RelativeLayout.LayoutParams.BELOWrule explicitly tells Android to position the scroll view directly below your "View Images" button. - We handle duplicate clicks by checking for an existing scroll view and removing it before adding a new one, so you won't get stacked views.
内容的提问来源于stack exchange,提问作者notakoba

