如何在 Android 中使用 XML 创建的布局放置以编程方式创建的水平滚动视图?
How to put programmatically created Horizontally Scrolling view with XML created Layout in Android?
在我的应用中,用户可以 select 来自图库的多张图片,然后他可以在应用内查看。
我在 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" />
为了显示我在 HorizontalScrollView
中以编程方式创建的 ImageView
数组设置为 WRAP_CONTENT
的图像,但问题是当我单击查看按钮 HorizontalScrollView
跨越整个屏幕像 this。
我想在视图按钮下方显示 HorizontalScrollView
。我该怎么做?
我是这样创建的 HorizontalScrollView
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);
}
}
这应该可以解决您的问题。
在我的应用中,用户可以 select 来自图库的多张图片,然后他可以在应用内查看。
我在 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" />
为了显示我在 HorizontalScrollView
中以编程方式创建的 ImageView
数组设置为 WRAP_CONTENT
的图像,但问题是当我单击查看按钮 HorizontalScrollView
跨越整个屏幕像 this。
我想在视图按钮下方显示 HorizontalScrollView
。我该怎么做?
我是这样创建的 HorizontalScrollView
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);
}
}
这应该可以解决您的问题。