态地在Android的一个Horizo​​ntalScrollV

态地在Android的一个Horizo​​ntalScrollV

本文介绍了动态地在Android的一个Horizo​​ntalScrollView插入视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发的Andr​​oid平板电脑3.0,有一个活动,应该是滚动水平轴,像一本电子书的应用程序。

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.

对于这一点,我用我的布局Horizo​​ntalScrollView内RelativeLayout的。下面是XML:

For that, I'm using a RelativeLayout inside a HorizontalScrollView on my layout. Here is the 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>

此XML文件名为main.xml中。

This xml file is called main.xml.

我在做什么在java文件是:

What I'm doing in the java file is:

setContentView(R.layout.main);
parent = (ViewGroup) findViewById(R.id.container);
View v = createView(); // Thats working for sure.
parent.addView(v);

但是它不工作,视图V没有显示在屏幕上。但是,如果我这样做

But it is not working, the view V doesn't show on the screen. But if i do

addContentView(v)

它添加视图v到屏幕(证明,我的方法的工作),但它是不滚动,因为它是Horizo​​ntalScrollView外面。我究竟做错了什么?

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. 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.

推荐答案

改变RelativeLayout的宽度为 WRAP_CONTENT

Change the width of Relativelayout to wrap_content.

尝试用这种方法来添加视图。

Try using this method to add the view.

void addView (View child, ViewGroup.LayoutParams params)

编辑:
删除安卓方向=横向 Horizo​​ntalScrollView


Remove android:orientation="horizontal" from the HorizontalScrollView

这篇关于动态地在Android的一个Horizo​​ntalScrollView插入视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 12:21