android include使用

好处可以重复利用包括标题栏


第一步在对应的主布局xml里面布局, 这里面的<include>对应的就是另外一个布局 ,对应的id

<RelativeLayout 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="wrap_content"
    android:layout_height="wrap_content"

    tools:context="com.example.samsung.ceshi.Main2Activity">

    <include
        android:id="@+id/header"
        layout="@layout/layout"
        />
<TextView
    android:layout_below="@+id/header"
    android:id="@+id/tv"
    android:text="测试"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />
<!--  <WebView
      android:id="@+id/img"
      android:layout_width="match_parent"
      android:layout_height="match_parent"></WebView>-->

</RelativeLayout>

 下面 我们来看 include 标签里面的对应的 布局  

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/one"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">
    <ImageView
        android:id="@+id/img"
        android:src="@mipmap/back"
        android:layout_width="50dp"
        android:layout_height="50dp" />

    <TextView
        android:layout_gravity="center"
        android:id="@+id/tv2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="95dp"
        android:layout_marginStart="95dp"
        android:layout_marginTop="13dp"
        android:layout_toEndOf="@+id/img"
        android:layout_toRightOf="@+id/img"
        android:text="界面"
        android:textColor="@color/colorPrimary"
        android:textSize="20dp"
        />

    <TextView
        android:textSize="20dp"
        android:textColor="@color/colorPrimary"
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:layout_alignParentRight="true"
        android:layout_alignTop="@+id/tv2"
        android:layout_marginEnd="24dp"
        android:layout_marginRight="24dp"
        android:text="完成" />

</RelativeLayout>

下面 我们去activity 进行操作 ,对应的文字赋值  点击跳转 ,改变颜色等  根据自己的需求 

 TextView tv,tv2;
 ImageView img;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        tv  = (TextView)findViewById(R.id.tv);
        tv2  = (TextView)findViewById(R.id.tv2);
       img = (ImageView)findViewById(R.id.img);
       tv2.setText("我的界面");

        RelativeLayout layoutHeader = (RelativeLayout) findViewById(R.id.header);
        layoutHeader.setBackgroundColor(Color.BLUE);
   img.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        Intent intent = new Intent(Main2Activity.this,MainActivity.class);
        startActivity(intent);
    }
});




猜你喜欢

转载自blog.csdn.net/bbtianshi/article/details/80263062