动态添加内容到RelativeLayout
由于我仍在学习 Android(亚马逊表示我需要 2 个月的时间才能收到《Hello,Android》一书),所以我仍在尝试做一些简单的事情。我可以使用 图像视图。创建它的代码如下:
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;
}
添加项目的代码为:
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 ) );
}
});
当我单击按钮时,所有新创建的视图都会被放置在彼此之上。我希望将它们放置在下一个元素的右侧。我在SO上快速搜索了与RelativeLayout相关的文章,发现了一些类似的文章(here ,此处,此处,以及此处),但是虽然这些解决了将内容放入相对视图的问题,但它们似乎没有解决定位方面的问题。
我做错了什么?
编辑:
我的主要 xml 如下所示:
<?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>
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>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
看起来您可能会将视图添加到布局 xml 的根目录而不是relativelayout。
你可以尝试:
It looks like you might be adding the view to the root of the layout xml instead of the RelativeLayout.
You could try:
您正在函数调用中创建新的相对布局。因此,每次创建新的相对布局并在单击按钮时将其添加到视图中。使用通用的相对布局。
you are creating new relative layout inside function call. So every time new relative layout created and it added in the view when click button . Use common relative layout.