搜索框

//搜索框布局(layout_search)
xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:padding="8dp"
    android:background="@drawable/search_bg"
    android:layout_height="wrap_content">

    <ImageView
        android:layout_width="@dimen/search_height"
        android:layout_height="@dimen/search_height"
        android:src="@drawable/a_4"/>

    <EditText
        android:layout_width="0dp"
        android:layout_height="@dimen/search_height"
        android:layout_weight="1"
        android:id="@+id/search_content"
        android:background="@null"/>
    <ImageView
        android:layout_width="@dimen/search_height"
        android:layout_height="@dimen/search_height"
        android:src="@drawable/root"/>

LinearLayout>
//自定义的搜索框

 
  

package com.example.mysearchview; /** * Created by lenovo on 2018/4/12. */ import android.content.Context; import android.support.annotation.Nullable; import android.util.AttributeSet; import android.view.View; import android.widget.EditText; import android.widget.LinearLayout; /** * 1.组合控件 * 将原有的控件组合起来成为一个整体---控件 *

* 1.根据需求,写布局文件 * 2.布局文件---View * 3.逻辑 *

* 2.自定义View * 3.自定义ViewGroup *

* 构造 * onDraw * 绘制,文字,图片,颜色,形状 * onMeasure * 测量 * onLayout * 布局 * onSizeChange * 当控件大小改变是调用 * onTouchEvent * 处理触摸事件 */ public class MySearchView extends LinearLayout{ private EditText search_content; public MySearchView(Context context) { this(context, null); } public MySearchView(Context context, @Nullable AttributeSet attrs) { this(context,attrs, 0); } public MySearchView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); View view = View. inflate(context, R.layout. layout_search, this); search_content = (EditText) view.findViewById(R.id. search_content); } public String getContent(){ return search_content.getText().toString(); }}

//主布局
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:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent" tools:context="com.example.mysearchview.MainActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_vertical"
        android:orientation="horizontal">

        <com.example.mysearchview.MySearchView
            android:layout_width="0dp"
            android:layout_height="45dp"
            android:layout_weight="1"
            android:id="@+id/mysearch">com.example.mysearchview.MySearchView>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/tv_search"
            android:text="搜索"/>
    LinearLayout>

LinearLayout>
//主方法

 
  

package com.example.mysearchview; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.TextView; import android.widget.Toast; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout. activity_main); final MySearchView mySearchView= (MySearchView) findViewById(R.id. mysearch); TextView textView = (TextView) findViewById(R.id. tv_search); textView.setOnClickListener( new View.OnClickListener() { @Override public void onClick(View v) { String content = mySearchView.getContent(); Toast. makeText(MainActivity. this,content,Toast. LENGTH_SHORT).show(); } }); }}
//values里的dimens布局
xml version="1.0" encoding="utf-8"?>
<resources>

    <dimen name="search_height">30dpdimen>

resources>

//draw able里的search——bg方法
xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">

    <size
        android:width="50dp"
        android:height="@dimen/search_height" />
    <solid android:color="#55999999" />
    <corners android:radius="18dp" />

shape>

你可能感兴趣的:(搜索框)