动态添加按钮问题

r00_a2lBUR 2018-07-17 09:57:35
我想在一个RelativeLayout动态添加几个按钮,排列是从右到左,第一个可以实现,请教论坛大牛们,第二个和第三个要怎么实现?

布局代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="动态添加按钮Demo"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="添加"
android:id="@+id/Button_Add"
android:onClick="e_View_Clicked"
/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/RelativeLayout_Test"
>

</RelativeLayout>

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="按钮一"
android:layout_alignParentRight="true"
android:id="@+id/Button_One"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="按钮二"
android:id="@+id/Button_Two"
android:layout_toLeftOf="@id/Button_One"
android:layout_alignTop="@id/Button_One"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="按钮三"
android:id="@+id/Button_Three"
android:layout_toLeftOf="@id/Button_Two"
android:layout_alignTop="@id/Button_One"
/>
</RelativeLayout>

</LinearLayout>


Activity代码:

package com.test.demo.demo_addbuttons;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

mRelativeLayoutTest = (RelativeLayout)findViewById(R.id.RelativeLayout_Test);
}

public void e_View_Clicked(View view){
int iViewID=view.getId();
switch (iViewID){
case R.id.Button_Add:
//动态添加按钮
f_add();
break;
}
}

private void f_add(){
RelativeLayout.LayoutParams vParams=new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
vParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
Button vButtonOne=new Button(this);
vButtonOne.setLayoutParams(vParams);
vButtonOne.setText("按钮一");
mRelativeLayoutTest.addView(vButtonOne);
//按钮二,按钮三要怎么做?
}

RelativeLayout mRelativeLayoutTest;
}

...全文
146 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
YXTS122 2018-07-25
  • 打赏
  • 举报
回复
动态添加按钮,找个时间我也做做。。。。。。。
r00_a2lBUR 2018-07-17
  • 打赏
  • 举报
回复
引用 1 楼 qq_28339011 的回复:
2种方案,不使用relativelayout
1. 使用LinearLayout,addview(layout,0);可以从右向左添加布局,不需要额外的设置.
2. 使用framelayout,通过设置对应的marginright去排列布局,relativelayout虽然也可以,但是framelayout的布局要简单许多
太帅了
bt侠 2018-07-17
  • 打赏
  • 举报
回复
2种方案,不使用relativelayout
1. 使用LinearLayout,addview(layout,0);可以从右向左添加布局,不需要额外的设置.
2. 使用framelayout,通过设置对应的marginright去排列布局,relativelayout虽然也可以,但是framelayout的布局要简单许多

80,351

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧