首先实现整体的排列位置,搜索栏,按钮,下面的八个图片和文字,新建一个空活动
布局文件如下所示:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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:theme="@style/Theme.AppCompat.NoActionBar"
tools:context=".HomeActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<!-- 搜索部分-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<FrameLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
>
<EditText
android:id="@+id/searchLan"
android:layout_width="match_parent"
android:layout_weight="1"
android:layout_height="49dp"
android:layout_gravity="center_vertical"
android:layout_margin="10dp"
android:paddingLeft="60dp"
android:background="@drawable/table_shape"
android:drawablePadding="1dp"
android:hint="请输入关键字"
android:imeOptions="actionSearch"
android:inputType="text"
android:textSize="16dp" >
</EditText>
<ImageView
android:layout_marginTop="17dp"
android:layout_marginLeft="20dp"
android:layout_width="35dp"
android:layout_height="35dp"
android:src="@drawable/sousuo"
>
</ImageView>
</FrameLayout>
<Button
android:id="@+id/search_btn_back"
android:layout_width="60dp"
android:layout_height="40dp"
android:layout_gravity="center_vertical"
android:layout_marginTop="10dp"
android:layout_marginRight="10dp"
android:layout_marginBottom="10dp"
android:background="@drawable/table_shape"
android:text="返回"
android:textColor="#333333" />
</LinearLayout>
<!--实体部分-->
<ScrollView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<!-- 滚动开始-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal">
<LinearLayout
android:id="@+id/linearLayout1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical">
<ImageView
android:id="@+id/one"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:src="@drawable/tianmaoxinpin"></ImageView>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="天猫新品"
android:textColor="#000000"
android:textSize="10sp">
</TextView>
</LinearLayout>
<LinearLayout
android:id="@+id/linearLayout2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical">
<ImageView
android:layout_width="wrap_content"
android:layout_height="50dp"
android:src="@drawable/jinribaokuan"></ImageView>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="今日爆款"
android:textColor="#000000"
android:textSize="10sp">
</TextView>
</LinearLayout>
<LinearLayout
android:id="@+id/linearLayout3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical">
<ImageView
android:layout_width="wrap_content"
android:layout_height="50dp"
android:src="@drawable/tianmaoguoji"></ImageView>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="天猫国际"
android:textColor="#000000"
android:textSize="10sp">
</TextView>
</LinearLayout>
<LinearLayout
android:id="@+id/linearLayout4"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical">
<ImageView
android:layout_width="wrap_content"
android:layout_height="50dp"
android:src="@drawable/tianmaonongchang"></ImageView>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="天猫农场"
android:textColor="#000000"
android:textSize="10sp">
</TextView>
</LinearLayout>
<LinearLayout
android:id="@+id/linearLayout5"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical">
<ImageView
android:layout_width="wrap_content"
android:layout_height="50dp"
android:src="@drawable/tianmaochaoshi"></ImageView>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="天猫超市"
android:textColor="#000000"
android:textSize="10sp">
</TextView>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
</LinearLayout>
<!-- /*****************************/-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal">
<LinearLayout
android:id="@+id/linearLayout6"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical">
<ImageView
android:layout_width="wrap_content"
android:layout_height="50dp"
android:src="@drawable/chongzhizhongxin"></ImageView>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="充值中心"
android:textColor="#000000"
android:textSize="10sp">
</TextView>
</LinearLayout>
<LinearLayout
android:id="@+id/linearLayout7"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical">
<ImageView
android:layout_width="wrap_content"
android:layout_height="50dp"
android:src="@drawable/jipiaojiudian"></ImageView>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="机票酒店"
android:textColor="#000000"
android:textSize="10sp">
</TextView>
</LinearLayout>
<LinearLayout
android:id="@+id/linearLayout8"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical">
<ImageView
android:layout_width="wrap_content"
android:layout_height="50dp"
android:src="@drawable/lintaojinbi"></ImageView>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="领淘金币"
android:textColor="#000000"
android:textSize="10sp">
</TextView>
</LinearLayout>
<LinearLayout
android:id="@+id/linearLayout9"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical">
<ImageView
android:layout_width="wrap_content"
android:layout_height="50dp"
android:src="@drawable/alipaimai"></ImageView>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="阿里拍卖"
android:textColor="#000000"
android:textSize="10sp">
</TextView>
</LinearLayout>
<LinearLayout
android:id="@+id/linearLayout10"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical">
<ImageView
android:layout_width="wrap_content"
android:layout_height="50dp"
android:src="@drawable/taobaochihuo"></ImageView>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="淘宝吃货"
android:textColor="#000000"
android:textSize="10sp">
</TextView>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
</LinearLayout>
<!-- /***************************开始下面的******************************/-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<!-- /*left frame start--------------*/-->
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical">
<!-- /*left three+++++++++++*/-->
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<!-- /*one left**********************/-->
<TextView
android:layout_width="wrap_content"
android:layout_height="20dp"
android:layout_gravity="top"
android:layout_marginLeft="10dp"
android:text="聚划算 01:23:56"
android:textColor="#000000"></TextView>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical">
<ImageView
android:layout_width="wrap_content"
android:layout_height="50dp"
android:src="@drawable/hhua" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="19.9元"
android:textColor="#000000"
android:textSize="15sp">
</TextView>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical">
<ImageView
android:layout_width="wrap_content"
android:layout_height="50dp"
android:src="@drawable/hhua"></ImageView>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="29.9元"
android:textSize="15sp">
</TextView>
</LinearLayout>
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="20dp"
android:layout_gravity="top"
android:layout_marginLeft="10dp"
android:text="百亿补贴 01:23:56"></TextView>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical">
<ImageView
android:layout_width="wrap_content"
android:layout_height="50dp"
android:src="@drawable/taobao"></ImageView>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="口罩19.9元"
android:textSize="15sp">
</TextView>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical">
<ImageView
android:layout_width="wrap_content"
android:layout_height="50dp"
android:src="@drawable/taobao"></ImageView>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="围脖9.9元"
android:textSize="15sp">
</TextView>
</LinearLayout>
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="20dp"
android:layout_gravity="top"
android:layout_marginLeft="10dp"
android:text="天天特卖 01:23:56"></TextView>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical">
<ImageView
android:layout_width="wrap_content"
android:layout_height="50dp"
android:src="@drawable/taobao"></ImageView>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="工厂直购"
android:textSize="15sp">
</TextView>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical">
<ImageView
android:layout_width="wrap_content"
android:layout_height="50dp"
android:src="@drawable/guagua"></ImageView>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="9.9包邮"
android:textSize="15sp">
</TextView>
</LinearLayout>
</LinearLayout>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1">
</LinearLayout>
</LinearLayout>
<!-- 占位控件-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="100dp"></LinearLayout>
<!--滚动内容-->
</LinearLayout>
</ScrollView>
<!-- 实体部分结束-->
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
java文件如下:
package com.example.mystore;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
import android.app.Activity;
public class HomeActivity extends AppCompatActivity implements View.OnClickListener{
private LinearLayout linearLayout1;
private LinearLayout linearLayout2;
private LinearLayout linearLayout3;
private LinearLayout linearLayout4;
private LinearLayout linearLayout5;
private LinearLayout linearLayout6;
private LinearLayout linearLayout7;
private LinearLayout linearLayout8;
private LinearLayout linearLayout9;
private LinearLayout linearLayout10;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
//注册监听器/
linearLayout1 = findViewById(R.id.linearLayout1);
linearLayout1.setOnClickListener(this);
linearLayout2 = findViewById(R.id.linearLayout2);
linearLayout2.setOnClickListener(this);
linearLayout3 = findViewById(R.id.linearLayout3);
linearLayout3.setOnClickListener(this);
linearLayout4 = findViewById(R.id.linearLayout4);
linearLayout4.setOnClickListener(this);
linearLayout5 = findViewById(R.id.linearLayout5);
linearLayout5.setOnClickListener(this);
linearLayout6 = findViewById(R.id.linearLayout6);
linearLayout6.setOnClickListener(this);
linearLayout7 = findViewById(R.id.linearLayout7);
linearLayout7.setOnClickListener(this);
linearLayout8 = findViewById(R.id.linearLayout8);
linearLayout8.setOnClickListener(this);
linearLayout9 = findViewById(R.id.linearLayout9);
linearLayout9.setOnClickListener(this);
linearLayout10 = findViewById(R.id.linearLayout10);
linearLayout10.setOnClickListener(this);
}
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.linearLayout1:
onClickLinearLayout(view);
break;
case R.id.linearLayout2:
onClickLinearLayout(view);
break;
case R.id.linearLayout3:
onClickLinearLayout(view);
break;
case R.id.linearLayout4:
onClickLinearLayout(view);
break;
case R.id.linearLayout5:
onClickLinearLayout(view);
break;
case R.id.linearLayout6:
onClickLinearLayout(view);
break;
case R.id.linearLayout7:
onClickLinearLayout(view);
break;
case R.id.linearLayout8:
onClickLinearLayout(view);
break;
case R.id.linearLayout9:
onClickLinearLayout(view);
break;
case R.id.linearLayout10:
onClickLinearLayout(view);
break;
}
}
private void onClickLinearLayout(View view) {
//处理逻辑
Toast.makeText(getApplicationContext(), "hello",Toast.LENGTH_SHORT).show();
}
}
按钮监听有三部曲
1 设置id
2 findViewById通过id找到视图
3 onClickLinearLayout()
button_begin=(Button)findViewById(R.id.button_begin);
button_exit=(Button)findViewById(R.id.button_exit);
Button.OnClickListener control=new View.OnClickListener() {
@Override
public void onClick(View view) {
switch (view.getId()){
case R.id.button_begin:
Intent game=new Intent(MainActivity.this,game_main.class);
startActivity(game);
break;
case R.id.button_exit:
System.exit(0);
break;
}
}
};
button_begin.setOnClickListener(control);
button_exit.setOnClickListener(control);