我有一个布局,我有3个文件夹和一个添加按钮 我想要的是用户点击添加按钮后立即。添加按钮位置的布局中添加了一个新文件夹,文件夹-3下方的添加按钮向下移动 任何建议如何创建此动态布局。目前我甚至没有一个可以实现这一点的黑客想法。
任何帮助都非常感谢。 谢谢。
答案 0 :(得分:0)
使用button = new Button(getApplicationContext())
创建按钮,而不是使用layout = (LinearLayout) findViewById (R.id.<yourLayoutId>)
获取viewGroup对象(我猜是LinearLayout),然后只需layout.addView(button)
。
答案 1 :(得分:0)
//the layout on which you are working
LinearLayout layout = (LinearLayout) findViewById(R.id.linear_layout_tags);
//set the properties for button
Button btnTag = new Button(this);
btnTag.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
btnTag.setText("Button");
btnTag.setId(some_random_id);
//add button to the layout
layout.addView(btnTag);
答案 2 :(得分:0)
试试这个:
LinearLayout layout = new LinearLayout(context);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.MATCH_PARENT);
Button btn = new Button(this);
btn .setLayoutParams(params);
layout.addView(btn);
答案 3 :(得分:0)
将GridView
与自定义适配器一起使用。
<?xml version="1.0" encoding="utf-8"?>
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/gridview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:columnWidth="90dp"
android:numColumns="auto_fit"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:stretchMode="columnWidth"
android:gravity="center" />
您需要一个自定义适配器来为网格视图充气。
保留一个计数器,说nbFolders
表示文件夹的数量。
int nbFolders = 0;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
GridView gridview = (GridView) findViewById(R.id.gridview);
gridview.setAdapter(new FolderAdapter());
gridview.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
if(position == nbFolders){
nbFolders++;
((FolderAdapter)gridView.getAdapter()).notifyDataSetChanged();
}
}
});
}
//Here is where the magic happens
public class FolderAdapter extends BaseAdapter {
public int getCount() {
return nbFolders + 1;
}
public View getView(int position, View convertView, ViewGroup parent) {
TextView yourView;
if (convertView == null) {
yourView = new TextView(YourActivity.this);
} else {
yourView = (TextView) convertView;
}
if(position < nbFolders){
yourView.setText("Folder " + (position + 1));
}else{
yourView.setText("Add");
}
return yourView;
}
}