我们知道写android的布局,分为代码和xml的方式。
一般我们大都是使用xml写布局,因为用xml写布局更直观和简单,方便观察效果和更改。但是也有缺点,那就是不能更改,比如你想把某个button更改为textview,用xml怎么写呢??
所以在这种情况下,就得使用代码来写布局了。
1.
更改 布局中的一个子布局里面的内容。
有3个布局文件,父容器布局,另外两个是点击效果的布局。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<RelativeLayout
android:id="@+id/relat_old"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</RelativeLayout>
</LinearLayout>
两个子布局文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<ImageView
android:id="@+id/img_old"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/nav_turn_via_1" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="old ,you see!!" />
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<ImageView
android:id="@+id/img_new"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/nav_turn_via_1" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="new ,you see!!" />
</LinearLayout>
</LinearLayout>
活动代码:
public class OldActivity extends Activity implements OnClickListener {
private RelativeLayout layout;
private ImageView oldImg;
private ImageView newImg;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.acy_testold);
init();
}
public void init() {
RelativeLayout re = new RelativeLayout(this);
layout = (RelativeLayout) findViewById(R.id.relat_old);
layout.addView(LayoutInflater.from(this).inflate(R.layout.acy_oldacy,
null));
oldImg = (ImageView) findViewById(R.id.img_old);
oldImg.setOnClickListener(this);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (v == oldImg) {
layout.removeAllViews();
layout.addView(LayoutInflater.from(this).inflate(
R.layout.acy_newacy, null));
Toast.makeText(this, "you click old!!", 1000).show();
newImg = (ImageView) findViewById(R.id.img_new);
newImg.setOnClickListener(this);
} else if (v == newImg) {
layout.removeAllViews();
layout.addView(LayoutInflater.from(this).inflate(
R.layout.acy_oldacy, null));
oldImg = (ImageView) findViewById(R.id.img_old);
oldImg.setOnClickListener(this);
Toast.makeText(this, "you click new!!", 1000).show();
}
}
}
效果:
2.
设置组件式可见或者不可见属性。
xml:
<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="1" />
<Button
android:id="@+id/btn_t"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="button"
android:visibility="visible" />
<TextView
android:id="@+id/tv_t"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:visibility="gone"
android:text="TextView" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="3" />
</LinearLayout>
</RelativeLayout>
代码:
public class MainActivity extends Activity implements OnClickListener {
private TextView tv2;
private Button btn2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
}
public void init() {
btn2 = (Button) findViewById(R.id.btn_t);
btn2.setOnClickListener(this);
btn2.setVisibility(View.VISIBLE);
tv2=(TextView) findViewById(R.id.tv_t);
tv2.setOnClickListener(this);
tv2.setVisibility(View.GONE);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (v == tv2) {
tv2.setVisibility(View.GONE);
btn2.setVisibility(View.VISIBLE);
} else if (v == btn2) {
btn2.setVisibility(View.GONE);
tv2.setVisibility(View.VISIBLE);
}
}
}
效果:
点击中间的组件,变为textview: