自定义标题栏

来源:互联网 发布:最好玩的网络手机游戏 编辑:程序博客网 时间:2024/06/10 13:01

实用自定义view实现简单标题栏

自定义一个类,继承RelativeLayout

public class TitleLayout extends RelativeLayout {    private ImageView left_img;    private ImageView right_img;    private TextView tv;    public TitleLayout(Context context) {        super(context);    }    public TitleLayout(Context context, AttributeSet attrs) {        super(context, attrs);        //加载自定义的布局  布局文件文字图片可以自行更改        View view = LayoutInflater.from(context).inflate(R.layout.title, this);        tv = view.findViewById(R.id.tv);        left_img = view.findViewById(R.id.left_img);        right_img = view.findViewById(R.id.right_img);        if (attrs != null) {            TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.TitleLayout);            String string = ta.getString(R.styleable.TitleLayout_titleText);            //第二个参数是设置默认图片            int leftImage = ta.getInt(R.styleable.TitleLayout_leftImage, R.mipmap.ic_launcher);            int rightImage = ta.getInt(R.styleable.TitleLayout_rightImage, R.mipmap.ic_launcher);            left_img.setImageResource(leftImage);            right_img.setImageResource(rightImage);            tv.setText(string);        }    }    //以下是代码中设置属性的方法可根据需求自行添加    public TitleLayout(Context context, AttributeSet attrs, int defStyleAttr) {        super(context, attrs, defStyleAttr);    }    public void setTitleText(String str) {        tv.setText(str);    }    public String getTitleText() {        return tv.getText().toString();    }    public void setLeftImage(int image) {        left_img.setImageResource(image);    }    public void setRightImage(int image) {        right_img.setImageResource(image);    }

}


布局文件:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="70dp"    android:background="#22ede3"    android:orientation="horizontal">    <ImageView        android:layout_margin="10dp"        android:id="@+id/left_img"        android:layout_width="50dp"        android:layout_height="50dp" />    <TextView        android:textSize="20sp"        android:textColor="#000"        android:id="@+id/tv"        android:layout_margin="10dp"        android:gravity="center"        android:layout_weight="1"        android:layout_width="0dp"        android:layout_height="50dp" />    <ImageView        android:id="@+id/right_img"        android:layout_margin="10dp"        android:layout_width="45dp"        android:layout_height="45dp" /></LinearLayout>activity中实用方法:
public class MainActivity extends AppCompatActivity {
private TitleLayout titleLayout;

@Overrideprotected void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentView(R.layout.activity_main);
    titleLayout = (TitleLayout) view.findViewById(R.id.titleLayout);
    //设置属性   titleLayout.setTitleText("Hello Kity");   titleLayout.setLeftImage(R.mipmap.ic_launcher);   titleLayout.setRightImage(R.mipmap.ic_launcher);
   }}