本人刚学Android不久,现在第一次在这里写文章,打算以后把平时积累的经验和方法写在这里,一是为了自己以后看,二是希望帮助像我一样对Android有浓烈兴趣的开发者,写的不好,还请大家见谅。这里要实现的功能也很简单,标题栏上边有一个添加按钮,每点击添加按钮时在最下面新增一行输入框,每个输入框后面有个删除按钮,点击删除按钮,对应的输入框消失。说这么多,比较啰嗦,下面直接上几张图比较直观,大家一看便懂。
1、这个功能的实现很简单,就是一个预加载的布局动画。首先建一个activity_main.xml的布局文件,具体代码如下:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#000000"
android:paddingBottom="5dip"
android:paddingRight="5dip"
android:paddingTop="5dip" >
<ImageView
android:id="@+id/add_new_view_iv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:src="@drawable/ic_action_new" />
</RelativeLayout>
<LinearLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
<span style="color:#ff0000;">android:animateLayoutChanges="true"</span>
android:orientation="vertical" >
</LinearLayout>
</LinearLayout>
2、接着在建一个每次新添加的view的xml,名为
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="?android:listPreferredItemHeightSmall"
android:orientation="horizontal"
android:showDividers="middle"
android:divider="?android:dividerVertical"
android:dividerPadding="8dp"
android:gravity="center">
<!-- Dummy text view that will display the name of a random country. -->
<EditText android:id="@android:id/text1"
style="?android:textAppearanceMedium"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:paddingLeft="?android:listPreferredItemPaddingLeft" />
<!-- A button that, when pressed, will delete this list item row from its container. -->
<ImageButton android:id="@+id/delete_button"
android:layout_width="48dp"
android:layout_height="match_parent"
android:src="@drawable/ic_list_remove"
android:background="?android:selectableItemBackground"
android:contentDescription="@string/action_remove_item" />
</LinearLayout>
这个一看就懂没有什么好说的
3、MainActivity
package com.example.layoutchangesdemo;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.ImageView;
public class MainActivity extends Activity implements OnClickListener
{
private String TAG = MainActivity.class.getName();
private ViewGroup container;//添加新view的容器
private ImageView newViewIV;//添加按钮
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
initView();
initData();
}
private void initView()
{
container = $(R.id.container);
newViewIV = $(R.id.add_new_view_iv);
newViewIV.setOnClickListener(this);
}
@Override
public void onClick(View v)
{
switch (v.getId())
{
case R.id.add_new_view_iv:
addItem();
break;
}
}
/**
* 添加新view
*/
private void addItem()
{
Log.d(TAG, "添加view");
final ViewGroup newView = (ViewGroup) LayoutInflater.from(this).inflate(
R.layout.list_item_example, container, false);
newView.findViewById(android.R.id.text1);
newView.findViewById(R.id.delete_button).setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
container.removeView(newView);
}
});
int count = container.getChildCount();
container.addView(newView, count);
}
@SuppressWarnings({ "unchecked" })
public <T> T $(int RESID)
{
return (T) findViewById(RESID);
}
}
小菜鸟一个,第一次发帖写的不好,请见谅。。。