实现透明状态栏,自定义搜索框,banner实现加载图片,搜索框在banner上

来源:互联网 发布:多功能助手软件 编辑:程序博客网 时间:2024/06/07 19:13
//加依赖compile 'com.youth.banner:banner:1.4.9'在values下的styles修改
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">


public class TitleView_homepage extends RelativeLayout {    private ImageView fg_homepage_zxingiv;    private TextView fg_homepage_zxingtv;    private RelativeLayout title_homepage_left;    private EditText title_homepage_et;    private ImageView fg_homepage_searchiv;    private ImageView title_homepage_assistant;    private RelativeLayout title_homepage_search;    private ImageView fg_homepage_msgiv;    private TextView fg_homepage_msgtv;    private RelativeLayout title_homepage_right;    private ImageView zXingIv;    private TextView zXingTv;    private ImageView msgIv;    private TextView msgTv;    private ImageView searchIv;    private EditText et;    public TitleView_homepage(Context context) {        super(context);        initView(context);    }    private void initView(Context context) {        zXingIv = findViewById(R.id.title_homepage_zxingiv);        zXingTv = findViewById(R.id.title_homepage_zxingtv);        msgIv = findViewById(R.id.title_homepage_msgiv);        msgTv = findViewById(R.id.title_homepage_msgtv);        searchIv = findViewById(R.id.title_homepage_searchiv);        et = findViewById(R.id.title_homepage_et);        LayoutInflater.from(context).inflate(R.layout.title_homepage_layout, this);    }    public TitleView_homepage(Context context, AttributeSet attrs) {        super(context, attrs);        initView(context);    }    public TitleView_homepage(Context context, AttributeSet attrs, int defStyleAttr) {        super(context, attrs, defStyleAttr);    }    public void TitlezxingListener(OnClickListener listener) {        findViewById(R.id.title_homepage_left).setOnClickListener(listener);    }    public void TitleMsgListener(OnClickListener listener) {        findViewById(R.id.title_homepage_right).setOnClickListener(listener);    }    public void setSearchListener(OnClickListener listener) {        findViewById(R.id.title_homepage_et).setOnClickListener(listener);    }    public void setAssistantListener(OnClickListener listener) {        findViewById(R.id.title_homepage_assistant).setOnClickListener(listener);    }    public void setImage() {        zXingIv.setImageResource(R.drawable.as4);        zXingTv.setTextColor(Color.BLACK);        msgIv.setImageResource(R.drawable.as2);        msgTv.setTextColor(Color.BLACK);        et.setBackgroundColor(Color.GRAY);        searchIv.setImageResource(R.drawable.as2);    }}




public class MainActivity extends AppCompatActivity {    private Banner banner;    private List<Integer> images = new ArrayList<>();//存放本地图片的ID    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);
// 透明状态栏getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);

 setContentView(R.layout.activity_main);
 
banner = (Banner) findViewById(R.id.fg_homepage_banner);
 
setBanner();
 }
 /** * 显示Banner */
private void setBanner() {
 /******显示本地图片********/
//图片加载器中需要用到的path;
 images.add(R.drawable.banner_a);
images.add(R.drawable.banner_b);
 
images.add(R.drawable.banner_c);
 images.add(R.drawable.banner_d);
//添加本地mipmap下的图片;
 banner.setImageLoader(new ImageLoader() {
@Override
 
public void displayImage(Context context, Object path, ImageView imageView) {
imageView.setImageResource((Integer) path);
}
 });
banner.setImages(images);
 
banner.start();
 }}



主页面布局
<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="match_parent"    android:layout_height="match_parent"    tools:context="com.example.lx_20171113.MainActivity">    <RelativeLayout        android:id="@+id/fg_homepage_title"        android:layout_width="match_parent"        android:layout_height="200dp">        <com.youth.banner.Banner            android:id="@+id/fg_homepage_banner"            android:layout_width="match_parent"            android:layout_height="match_parent"            >        </com.youth.banner.Banner>        <com.example.lx_20171113.TitleView_homepage            android:id="@+id/fg_homepage_head"            android:layout_width="match_parent"            android:layout_height="80dp"></com.example.lx_20171113.TitleView_homepage>    </RelativeLayout></RelativeLayout>




title_homepage_layout布局
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"                android:layout_width="match_parent"                android:layout_height="match_parent"                android:orientation="vertical"                android:layout_marginTop="20dp"    >    <RelativeLayout        android:id="@+id/title_homepage_left"        android:layout_width="52dp"        android:layout_height="match_parent"        android:layout_centerVertical="true"        >        <ImageView            android:id="@+id/title_homepage_zxingiv"            android:layout_width="36dp"            android:layout_height="36dp"            android:layout_centerHorizontal="true"            android:src="@drawable/as5"            />        <TextView            android:id="@+id/title_homepage_zxingtv"            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:layout_alignParentBottom="true"           android:gravity="center"            android:text="扫啊扫"            android:textSize="10sp"            android:textColor="#fff"            />    </RelativeLayout>    <RelativeLayout        android:id="@+id/title_homepage_search"        android:layout_width="match_parent"        android:layout_height="match_parent"        android:layout_toLeftOf="@+id/title_homepage_right"        android:layout_toRightOf="@+id/title_homepage_left"        android:gravity="center"        android:layout_centerVertical="true"        >        <EditText            android:id="@+id/title_homepage_et"            android:editable="false"            android:layout_width="match_parent"            android:layout_height="36dp"            android:layout_centerVertical="true"            android:background="@drawable/corners_bg"            android:hint="联想笔记本电脑"            android:textColorHint="#999"            android:paddingLeft="60dp"            android:focusable="false"            />        <ImageView            android:id="@+id/title_homepage_searchiv"            android:layout_width="26dp"            android:layout_height="26dp"            android:src="@drawable/as6"            android:layout_centerVertical="true"            android:layout_marginLeft="16dp"            />        <ImageView            android:layout_width="26dp"            android:layout_height="26dp"            android:id="@+id/title_homepage_assistant"            android:src="@drawable/as1"            android:layout_centerVertical="true"            android:layout_marginRight="16dp"            android:layout_alignParentRight="true"            />    </RelativeLayout>    <RelativeLayout        android:id="@+id/title_homepage_right"        android:layout_width="52dp"        android:layout_height="match_parent"        android:layout_alignParentRight="true"        android:layout_centerVertical="true"        >        <ImageView            android:id="@+id/title_homepage_msgiv"            android:layout_width="36dp"            android:layout_height="36dp"            android:layout_centerHorizontal="true"            android:src="@drawable/as3"            />        <TextView            android:id="@+id/title_homepage_msgtv"            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:layout_alignParentBottom="true"            android:textSize="10sp"            android:text="消息"            android:textColor="#fff"            android:gravity="center"            />    </RelativeLayout></RelativeLayout>

corners_bg在drawable下创建
<shape xmlns:android="http://schemas.android.com/apk/res/android">    <!--设置填充颜色  为白色-->    <solid android:color="#ffffff"></solid>    <corners android:radius="50mm"/></shape>





原创粉丝点击