自定义view

自定义view (仿微信箭头)

2019-06-23  本文已影响0人  总会颠沛流离
biao.jpg

废话少说效果图(能够高效体提高布局得复用性)

image.png

第一步

INtemView.java 中继承extends RelativeLayout

  package com.example.xue;

  import android.content.Context;
  import android.os.Build;
  import android.support.annotation.NonNull;
  import android.support.annotation.RequiresApi;
  import android.util.AttributeSet;
  import android.view.LayoutInflater;
  import android.view.View;
  import android.widget.ImageView;
  import android.widget.RelativeLayout;
  import android.widget.TextView;

 public class INtemView extends RelativeLayout {
private TextView mLeftTv;
private TextView mRightTv;
private ImageView mIamgeTv;

public INtemView(Context context) {
    super(context);

}



public INtemView(Context context, AttributeSet attrs) {
    super(context, attrs);
    LayoutInflater.from(context).inflate(R.layout.item_layout, this);
    mLeftTv = (TextView)findViewById(R.id.tv_Left);
    mRightTv = (TextView) findViewById(R.id.tv_Right);
    mIamgeTv = (ImageView) findViewById(R.id.tv_iamge);

}
public void setView(String tvLeft,String tvRight,boolean isShow){
    if (tvLeft!=null) {
        mLeftTv.setText(tvLeft);
    }

    if (mRightTv!=null) {
        mRightTv.setText(tvRight);
    }
    if (isShow) {
        mIamgeTv.setVisibility(VISIBLE);
    } else {
        mIamgeTv.setVisibility(GONE);
    }


}







public INtemView(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
}

@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
public INtemView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
    super(context, attrs, defStyleAttr, defStyleRes);
}


}

第二步

item_layout.xml

 <?xml version="1.0" encoding="utf-8"?>
  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="wrap_content">
>



<RelativeLayout
android:background="@drawable/select_item"
android:layout_width="match_parent"
android:padding="10dp"
android:layout_height="wrap_content">
<TextView
android:layout_alignParentLeft="true"
android:text="我是标题"
android:layout_centerVertical="true"
android:id="@+id/tv_Left"
android:textSize="15dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
  <LinearLayout
    android:layout_centerVertical="true"
    android:layout_width="wrap_content"

    android:layout_alignParentRight="true"
    android:layout_height="wrap_content">
    <TextView
        android:id="@+id/tv_Right"
        android:text="我是标题"

        android:textSize="15dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
      <ImageView
        android:src="@mipmap/xue"
        android:id="@+id/tv_iamge"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />


        </LinearLayout>

         </RelativeLayout>
    <View
    android:layout_width="match_parent"
    android:layout_height="1dp"
    android:background="#E61212"
    ></View>

    </LinearLayout>
image.png

activity_main.xml 中复用

   <?xml version="1.0" encoding="utf-8"?>
  <LinearLayout 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="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
 <com.example.xue.INtemView
     android:id="@+id/item"
    android:layout_width="match_parent"
     android:layout_height="wrap_content"></com.example.xue.INtemView>
<com.example.xue.INtemView
    android:id="@+id/item1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"></com.example.xue.INtemView>

<com.example.xue.INtemView
    android:id="@+id/item2"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"></com.example.xue.INtemView>

  </LinearLayout>
image.png

第三步

MainActivity

public class MainActivity extends AppCompatActivity {

private INtemView mItem;
private INtemView mItem1;
private INtemView mItem2;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    initView();
}

private void initView() {
    mItem = (INtemView) findViewById(R.id.item);
    mItem.setView("薛志辉","28",true);
    mItem1 = (INtemView) findViewById(R.id.item1);
    mItem1.setView("薛志辉1","38",false);
    mItem2 = (INtemView) findViewById(R.id.item2);
    mItem2.setView("薛志辉2","38",true);

  }




}
上一篇 下一篇

猜你喜欢

热点阅读