2012-11-03 33 views
1

我发民与滚动型的应用程序在它被添加到一个的onCreate是RelativeLayout的大部分意见。就像这样: enter image description here添加视图作为第一个孩子

我也想在运行时添加视图。这些被添加到上图中的当前位置。我想要做的就是在运行时添加在所需位置视图。

我该如何做到这一点?

回答

0

本集为滚动视图 android:orientation="vertical"android:scrollbars="vertical"

+0

屏幕实际上是在landscapemode中,滚动视图从屏幕的顶部到底部。 java中定义的滚动和真实布局。你知道如何解决这个问题吗? – Magakahn

0

您可以将RelativeLayout的转换成的LinearLayout并设置android:orientation="vertical"

在这个LinearLayout中,你可以嵌套其他的LinearLayout您与android:orientation="horizontal"水平的看法。

通过这种方式,对于每个横向视图部分,您可以添加一个孩子的LinearLayout以你的父母的LinearLayout和每个孩子都将根据您的需要进行垂直布置一个在另一个之下。

+0

阅读上述 – Magakahn

1

我认为答案包含在最后一行(parent.addView(brick,);),但是为了排除我误解我已经发布的所有代码。 现在,我做同样的

XML
activity_view.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_height="match_parent" 
    android:layout_width="match_parent" 
> 
    <ScrollView 
     android:layout_height="wrap_content" 
     android:layout_width="wrap_content" 
    > 
     <LinearLayout android:id="@+id/parent" 
      android:layout_height="wrap_content" 
      android:layout_width="wrap_content" 
      android:orientation="horizontal" 
     > 
     </LinearLayout> 
    </ScrollView> 
</RelativeLayout> 

tpl_yellow_brick.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_height="42dp" 
    android:layout_width="42p" 
> 
    <TextView 
     android:layout_height="wrap_content" 
     android:layout_width="wrap_content" 
     android:text="I'm square yellow brick"    
    /> 
</LinearLayout> 

的Java

View brick = getLayoutInflater().inflate(R.layout.tpl_yellow_brick, null); 
ViewGroup parent = findViewById(R.id.parent); 
parent.addView(brick, 0); 
+0

Awesomeeeee我的最新评论...就像一个魅力 –

相关问题