[英]Insert a view dynamically in a HorizontalScrollView in Android
I'm developing an application for Android tablet 3.0 that has one activity that should be scrollable in the horizontal axis, like an e-book. 我正在为Android平板电脑3.0开发一个应用程序,它有一个应该可以在水平轴上滚动的活动,就像电子书一样。
For that, I'm using a RelativeLayout inside a HorizontalScrollView on my layout. 为此,我在我的布局上使用HorizontalScrollView中的RelativeLayout。 Here is the XML:
这是XML:
<?xml version="1.0" encoding="utf-8"?>
<HorizontalScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="800px"
android:id="@+id/horizontalScroll"
android:background="#C6D7D2" android:layout_height="600px">
<RelativeLayout android:id="@+id/container" android:layout_width="fill_parent" android:layout_height="fill_parent">
</RelativeLayout>
</HorizontalScrollView>
This xml file is called main.xml. 此xml文件名为main.xml。
What I'm doing in the java file is: 我在java文件中做的是:
setContentView(R.layout.main);
parent = (ViewGroup) findViewById(R.id.container);
View v = createView(); // Thats working for sure.
parent.addView(v);
But it is not working, the view V doesn't show on the screen. 但它不起作用,视图V没有显示在屏幕上。 But if i do
但如果我这样做
addContentView(v)
it adds the view v to the screen (the proof that my method works), but it isn't scrollable since it is outside the HorizontalScrollView. 它将视图v添加到屏幕(我的方法工作的证明),但它不可滚动,因为它在HorizontalScrollView之外。 What am I doing wrong?
我究竟做错了什么?
UPDATE: I tried with that and it also didn't work out: 更新:我试过这个,它也没有成功:
setContentView(R.layout.main);
LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
ViewGroup parent = (ViewGroup) findViewById(R.id.container);
View v = new View(this);
v.setBackgroundColor(Color.BLUE);
parent.addView(v,params);
I don't get a blue background. 我没有蓝色背景。
HorizontalScrollView scrollView = (HorizontalScrollView) findViewById(R.id.scrollView1);
LinearLayout topLinearLayout = new LinearLayout(this);
// topLinearLayout.setLayoutParams(android.widget.LinearLayout.LayoutParams.FILL_PARENT,android.widget.LinearLayout.LayoutParams.FILL_PARENT);
topLinearLayout.setOrientation(LinearLayout.HORIZONTAL);
for (int i = 0; i < 15; i++){
final ImageView imageView = new ImageView (this);
imageView.setTag(i);
imageView.setImageResource(R.drawable.ic_launcher);
topLinearLayout.addView(imageView);
imageView.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
// TODO Auto-generated method stub
Log.e("Tag",""+imageView.getTag());
}
});
}
scrollView.addView(topLinearLayout);
// ImageView img=(ImageView)findViewById(R.id.imageView1);
// final ImageLoader imageLoader = ImageLoader.getInstance();
//
//
//
// ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(getApplicationContext())
// .threadPoolSize(3)
// .threadPriority(Thread.NORM_PRIORITY - 2)
// .memoryCacheSize(1500000) // 1.5 Mb
// .discCacheSize(50000000) // 50 Mb
// .httpReadTimeout(10000) // 10 s
// .denyCacheImageMultipleSizesInMemory()
// .enableLogging() // Not necessary in common
// .build();
// // Initialize ImageLoader with configuration.
// ImageLoader.getInstance().init(config);
//
// final DisplayImageOptions options = new DisplayImageOptions.Builder()
// .showStubImage(R.drawable.ic_launcher)
// .cacheInMemory()
// .cacheOnDisc()
// .build();
//
// imageLoader.displayImage("http://3.bp.blogspot.com/_Sd45ASngYHA/TVC78RORKoI/AAAAAAAAARk/y0GcNkTmb40/s1600/android+logo1.jpg",img,options);
//
// img.setOnClickListener(new OnClickListener()
// {
//
// @Overridt
// public void onClick(View v)
// {
// // TODO Auto-generated method stub
// Dialog d =new Dialog(TestActivity.this);
// d.setContentView(R.layout.dialog);
//
// d.setCancelable(true);
//
// ImageView d_img=(ImageView)d.findViewById(R.id.dialog_img);
//// d.setLayoutParams(L)
// imageLoader.displayImage("http://3.bp.blogspot.com/_Sd45ASngYHA/TVC78RORKoI/AAAAAAAAARk/y0GcNkTmb40/s1600/android+logo1.jpg",d_img,options);
//
// d.show();
// }
// });
Change the width of Relativelayout to wrap_content
. 将Relativelayout的宽度更改为
wrap_content
。
Try using this method to add the view. 尝试使用此方法添加视图。
void addView (View child, ViewGroup.LayoutParams params)
EDIT: 编辑:
Remove android:orientation="horizontal"
from the HorizontalScrollView
从
HorizontalScrollView
删除android:orientation="horizontal"
you add the view into the relativelayout not into the horizontalscrollview using parent 您可以使用parent将视图添加到relativelayout而不是horizontalscrollview中
try with the horizontalscrollview object as you can done with the relativelayout 尝试使用horizontalscrollview对象,就像使用relativelayout一样
Try this code.
ImageView imageView = new ImageView(this);
imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
imageView.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
imageView.setBackgroundColor(Color.blue);
parent.addView(v,params);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.