动态地添加内容RelativeLayout的内容、动态、RelativeLayout

2023-09-05 08:57:16 作者:Thorns(荆棘)

由于我还是刚学的Andr​​oid(它似乎亚马逊表示,它会在2个月,直到我拿到你好,Android的书),我仍然在做简单的事情打转转。我没有问题,得到一个图标,显示一个按钮,在我的RelativeLayout的使用点击ImageView的。在code创建其计算方法如下:

 私人INT mIconIdCounter = 1;
私人ImageView的addIcon(){
    ImageView的项目=新ImageView的(这一点);
    item.setImageResource(R.drawable.tiles);
    item.setAdjustViewBounds(真正的);
    RelativeLayout.LayoutParams PARAMS =新RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
    如果(mIconIdCounter!= 1){
        params.addRule(RelativeLayout.RIGHT_OF,1);
    }
    item.setLayoutParams(PARAMS);
    item.setId(mIconIdCounter);
    ++ m_IconIdCounter;
    归还物品;
}
 

和code添加的项目是:

 按钮Add按钮=(按钮)findViewById(R.id.add_new);
addButton.setOnClickListener(新OnClickListener(){
    @覆盖
    公共无效的onClick(视图查看){
        addContentView(addIcon(),新RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
    }
});
 

当我点击我的按钮,会发生什么情况是所有新创建的视图都放在彼此顶上。我希望他们被放置到下一个元素的右边。我做了这么快速搜索与RelativeLayout的文章,发现一些相似(here, here, here,和here)但同时,这些解决获取内容到RelativeView他们似乎没有解决定位方面。

我是什么做错了吗?

编辑:

我的主XML是这样的:

 < XML版本=1.0编码=UTF-8&GT?;
< RelativeLayout的的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =FILL_PARENT
    >
    <按钮
        机器人:ID =@ + ID / add_new
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:文本=@字符串/ add_new
        机器人:layout_alignParentBottom =真/>
< / RelativeLayout的>
 
怎么避免RelativeLayout中的内容被挤出屏幕

解决方案

看起来你可能会添加视图布局XML,而不是RelativeLayout的根。

您可以尝试:

  RelativeLayout的布局=(RelativeLayout的)findViewById(R.id.my_layout);
layout.addView(addIcon());
 

Since I'm still just learning Android (and it appears Amazon says it'll be 2 months till I get the Hello, Android book) I'm still playing around with doing simple things. I have no problem getting an icon to display with the click of a button on my RelativeLayout using ImageView. The code for creating it is as follows:

private int mIconIdCounter = 1;
private ImageView addIcon(){
    ImageView item = new ImageView(this);
    item.setImageResource( R.drawable.tiles );
    item.setAdjustViewBounds(true);
    RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams( LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT );
    if( mIconIdCounter != 1 ){
        params.addRule(RelativeLayout.RIGHT_OF, 1 );
    }
    item.setLayoutParams( params );
    item.setId( mIconIdCounter );
    ++m_IconIdCounter;
    return item;
}

and the code to add the item is:

Button addButton = (Button)findViewById(R.id.add_new);
addButton.setOnClickListener( new OnClickListener(){
    @Override
    public void onClick(View view) {
        addContentView( addIcon(), new RelativeLayout.LayoutParams( LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT ) );
    }
});

When I click my button what happens is all the newly created views are placed atop one another. I'd like them to be placed to the right of the next element. I did a quick search on SO for articles relating to RelativeLayout and found some that were similar (here, here, here, and here) but while these addressed getting the content into the RelativeView they didn't seem to address the positioning aspect.

What am I doing wrong?

EDIT:

My main xml looks like:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
    <Button
        android:id="@+id/add_new"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/add_new"
        android:layout_alignParentBottom="true" />
</RelativeLayout>

解决方案

It looks like you might be adding the view to the root of the layout xml instead of the RelativeLayout.

You could try:

RelativeLayout layout = (RelativeLayout)findViewById(R.id.my_layout);
layout.addView(addIcon());