Android 添加视图仅动态显示背景

Android 添加视图仅动态显示背景,android,android-layout,Android,Android Layout,我使用了线性布局,它将有顶部布局和底部布局。底部的布局是scrollview,它也有listview。 我以编程方式添加了如下布局 protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); topview=getLayoutInflater().inflate(R.layout.details,null); scrollview=getLayoutInf

我使用了线性布局,它将有顶部布局和底部布局。底部的布局是scrollview,它也有listview。 我以编程方式添加了如下布局

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    topview=getLayoutInflater().inflate(R.layout.details,null);
    scrollview=getLayoutInflater().inflate(R.layout.scrollingrelativelayout,null);
    listview=getLayoutInflater().inflate(R.layout.listviewxml,null);
    lv=(ListView)listview.findViewById(R.id.listView2);
    lv.addHeaderView(scrollview);
    LinearLayout forscrolltest=new LinearLayout(this);
    forscrolltest.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT));
    forscrolltest.setOrientation(LinearLayout.VERTICAL);
    forscrolltest.addView(topview);
    forscrolltest.addView(lv);
    setContentView(forscrolltest);
我的xml代码是


在这里,当我运行这些布局时,这些布局中使用的文本视图不可见。我的代码有问题吗?请引导我

谢谢


默认情况下,ListView是可滚动的。不要将ScrollView放入ListView。因此,删除scrollview并使列表换行内容


快乐编码

如果要动态创建视图,则不要先在
onCreate
中使用
setContentView()
,如果要创建视图,则使用LayoutInflater膨胀布局,然后从中提取视图

对于当前代码,我看到的问题是
活动
视图设置了两次,导致工作量过大

relativeLayoutbottom.removeAllViews(); 
选中上面的一行,您将从布局中删除所有视图并将其添加到
ScrollView
<代码>s.addView(相对的LayoutBottom)


请检查您的版式通行证的标题是否正确。

谢谢大家。最后我找到了一个解决方案:

我为顶部布局、listview和listview上方的相对布局创建了单独的布局。然后我将相对布局添加为headerview,而不是使用scrollview,如下所示:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    topview=getLayoutInflater().inflate(R.layout.details,null);
    scrollview=getLayoutInflater().inflate(R.layout.scrollingrelativelayout,null);
    listview=getLayoutInflater().inflate(R.layout.listviewxml,null);
    lv=(ListView)listview.findViewById(R.id.listView2);
    lv.addHeaderView(scrollview);
    LinearLayout forscrolltest=new LinearLayout(this);
    forscrolltest.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT));
    forscrolltest.setOrientation(LinearLayout.VERTICAL);
    forscrolltest.addView(topview);
    forscrolltest.addView(lv);
    setContentView(forscrolltest);
它正在工作。:)


为什么不先在XML中设置布局呢?我在scrollview中使用了listview。我只是想解决这个问题。我认为“动态添加视图可能会有帮助”。这就是为什么。如果你的意思是
列表视图在
滚动视图中不起作用,那么它就不会起作用。无论如何,我们不知道
详细信息
布局是什么样子的,所以我们无法真正告诉您当您在代码中到处乱翻时发生了什么。这对我不起作用。因此,我正在尝试一种新的方式,但我需要整个页面应该滚动。而不仅仅是列表视图。
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    topview=getLayoutInflater().inflate(R.layout.details,null);
    scrollview=getLayoutInflater().inflate(R.layout.scrollingrelativelayout,null);
    listview=getLayoutInflater().inflate(R.layout.listviewxml,null);
    lv=(ListView)listview.findViewById(R.id.listView2);
    lv.addHeaderView(scrollview);
    LinearLayout forscrolltest=new LinearLayout(this);
    forscrolltest.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT));
    forscrolltest.setOrientation(LinearLayout.VERTICAL);
    forscrolltest.addView(topview);
    forscrolltest.addView(lv);
    setContentView(forscrolltest);