1 在res/layout 中新建一个xxx.xml布局文件
2.在新建的布局文件中填写想要的布局内容(如下)
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
android:id="@+id/banner"
android:background="#000000"
android:layout_width="320dp"
android:layout_height="50dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"/>
</RelativeLayout>
3.通过下面的方式完成
public class MainActivityextends AppCompatActivity {
private ViewmBanner =null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void buttonOnClick(View view) {
if (mBanner==null){
LayoutInflater inflater = LayoutInflater.from(MainActivity.this);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
mBanner = inflater.inflate(R.layout.banner_layout, null);
this.addContentView(mBanner,lp);
}else {
if (mBanner.getVisibility()==View.INVISIBLE){
mBanner.setVisibility(View.VISIBLE);
}else {
mBanner.setVisibility(View.INVISIBLE);
}
}
}
}
网友评论