在HorizontalScrollView中创建ImageView和TextView



我想在我的android应用程序的ImageView上创建一个TextView。我知道我需要一个RelativeLayout为此,但我不知道如何创建这个在我的主要。java(不是在XML)。它应该是动态的,所以我想创建一个for循环,创建许多imageview与TextView在它的一个HorizontalScrollView。

这是我的方法

LinearLayout sv = (LinearLayout) findViewById (R.id.LinearLayout1);

    for (int i=1 ; i<=3; i++){
        String uri = "drawable/test"; //only one picture several times
        int imageResource = getResources().getIdentifier(uri, null, getPackageName());
        ImageView iv = new ImageView (this);
        iv.setBackgroundResource (imageResource);
        sv.addView(iv);
    }

这只添加ImageViews到我的HorizontalScrollView (LinearLayout位于HorizontalScrollView),但现在我也想在我的ImageViews上添加TextViews。我试了很多方法,但都没用。我绝望。

希望有人能帮助我

PS:很抱歉我的拼写错误,我来自德国

如果你知道用xml设计,创建一个xml并扩展它。

下面的代码解释了如何使用它。

<<p> 代码片段/strong>
//Parent View
LinearLayout sv = (LinearLayout) findViewById (R.id.LinearLayout1); 
//infalter service
LayoutInflater inflater = (LayoutInflater) getActivity()
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//Loop to create all the child views
for (int i=0; i<=3; i++) {
//Refrence your layout xml here
LinearLayout childView= inflater. inflates (R. layout. your_xml, null);
//Get refrence to your button and imageview using childView
//add child view 
sv.addView(childView);
}
//add sv to horizontal scroll view

兄弟试试自定义视图。创建一个XML文件并创建一个基本布局。然后你想充气多少次就充气多少次不要用for循环。我认为你是android的新手。

http://androidexample.com/Dynamically_Create_View_Elements__-_Android_Example/index.php?view=article_discription&援助= 115,aaid = 137

最新更新