Android(一)

本文深入讲解Android开发中常用的UI组件,包括线性布局、相对布局、TextView、Button、EditText、RadioButton、CheckBox、ImageView等,以及如何使用Glide库加载图片资源,提供了丰富的代码示例和实践指导。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Android

1. AndroidStudio的安装与卸载

安装后阿里镜像配置(全局配置)

  • 第一步:在C:\Users\用户.gradle里面新建init.gradle文件(内容如下)
allprojects{
    repositories {
        def ALIYUN_REPOSITORY_URL = 'http://maven.aliyun.com/nexus/content/groups/public'
        def ALIYUN_JCENTER_URL = 'http://maven.aliyun.com/nexus/content/repositories/jcenter'
        all { ArtifactRepository repo ->
            if(repo instanceof MavenArtifactRepository){
                def url = repo.url.toString()
                if (url.startsWith('https://repo1.maven.org/maven2')) {
                    project.logger.lifecycle "Repository ${repo.url} replaced by $ALIYUN_REPOSITORY_URL."
                    remove repo
                }
                if (url.startsWith('https://jcenter.bintray.com/')) {
                    project.logger.lifecycle "Repository ${repo.url} replaced by $ALIYUN_JCENTER_URL."
                    remove repo
                }
            }
        }
        maven {
            url ALIYUN_REPOSITORY_URL
            url ALIYUN_JCENTER_URL
        }
    }
}
  • 第二步:修改工程里面的build.gradle配置
buildscript {
    repositories {
        maven {
            url "http://maven.aliyun.com/nexus/content/groups/public/"
        }
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:2.3.3'

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        maven {
            url "http://maven.aliyun.com/nexus/content/groups/public/"
        }
        jcenter()
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

2. UI组件

布局里面分为:线性布局、相对布局、表格布局、帧布局
其中最为常用的是:线性布局、相对布局

2.1 线性布局(LinearLayout)

在这里插入图片描述

2.2 相对布局(RelativeLayout)

在这里插入图片描述

案例:做成如下图案(综合线性布局和相对布局嵌套使用)
  • 如图
    在这里插入图片描述- 代码实现
<?xml version="1.0" encoding="utf-8"?>
<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"
    android:orientation="vertical"
    tools:context="com.xiaozhuan.helloworld.MainActivity">

    <!--android:orientation="vertical"-->
    <!-- 上局是将线性布局进行竖直排列(vertical),水平排列为horizontal -->

    <View
        android:id="@+id/view_1"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:background="#000000"
        />
    <View
        android:id="@+id/view_2"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:background="#FF0033"
        android:layout_below="@id/view_1"
        />
    <LinearLayout
        android:id="@+id/ll_1"
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:orientation="horizontal"
        android:layout_below="@id/view_2"
        android:background="#3300FF"
        android:padding="15dp"
        >

        <View
            android:id="@+id/view_3"
            android:layout_width="100dp"
            android:layout_height="match_parent"
            android:background="#FF0033"/>

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="#F7F709"
            android:padding="10dp"
            >
            <View
                android:id="@+id/view_4"
                android:layout_width="100dp"
                android:layout_height="match_parent"
                android:background="#09F768"
                android:layout_marginRight="10dp"
                />

            <View
                android:id="@+id/view_5"
                android:layout_width="100dp"
                android:layout_height="match_parent"
                android:background="#09F768"
                android:layout_toRightOf="@id/view_4"
                />
        </RelativeLayout>
    </LinearLayout>
</RelativeLayout>

2.3 TextView

  • 常用的功能有:

    1. 显示不下使用…
    2. 文字+icon
    3. 中划线、下划线
    4. 跑马灯
  • 进行Activity跳转到TextViewActivity页面代码实现

注意:新建Activity后需要在AndroidMainfest.xml中注册后才可以使用(AS软件已经在新建的时候自动注册好了)

    private Button mbtnButton;

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

        mbtnTextView = (Button) findViewById(R.id.btn_textview);
        mbtnTextView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //跳转到TextViewActivity演示页面
                Intent intent = new Intent(MainActivity.this,TextViewActivity.class);
                startActivity(intent);
            }
        });
    }
TextView中的主要功能实现案例
  • 效果图如下
    在这里插入图片描述
  • 代码实现
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    >

    <!-- 文字大小、颜色 -->
    <TextView
        android:id="@+id/tv_1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/app_tv_1"
        android:textColor="#000000"
        android:textSize="24sp"
        android:layout_margin="15dp"
        />

    <!-- 显示不下使用... -->
    <TextView
        android:id="@+id/tv_2"
        android:layout_width="100dp"
        android:layout_height="wrap_content"
        android:maxLines="1"
        android:ellipsize="end"
        android:text="小传在奔跑"
        android:textColor="#000000"
        android:textSize="24sp"
        android:layout_marginLeft="15dp"
        />

    <!-- 文字+icon -->
    <TextView
        android:id="@+id/tv_3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:drawablePadding="5dp"
        android:drawableRight="@drawable/icon_arrow"
        android:text="筛选"
        android:textColor="#000000"
        android:textSize="24sp"
        android:layout_marginLeft="15dp"
        android:layout_marginTop="15dp"
        />

    <!-- 中划线 -->
    <TextView
        android:id="@+id/tv_4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="小传在奔跑"
        android:textColor="#000000"
        android:textSize="24sp"
        android:layout_marginLeft="15dp"
        android:layout_marginTop="15dp"
        />

    <!-- 下划线 -->
    <TextView
        android:id="@+id/tv_5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="小传在奔跑"
        android:textColor="#000000"
        android:textSize="24sp"
        android:layout_marginLeft="15dp"
        android:layout_marginTop="15dp"
        />

    <!-- 下划线 -->
    <TextView
        android:id="@+id/tv_6"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#000000"
        android:textSize="24sp"
        android:layout_marginLeft="15dp"
        android:layout_marginTop="15dp"
        />

    <!-- 跑马灯 -->
    <TextView
        android:id="@+id/tv_7"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#FFFF00"
        android:text="小传在奔跑 小传在奔跑 小传在奔跑"
        android:textColor="#FF0000"
        android:textSize="24sp"
        android:singleLine="true"
        android:ellipsize="marquee"
        android:marqueeRepeatLimit="marquee_forever"
        android:focusable="true"
        android:focusableInTouchMode="true"
        android:layout_marginLeft="15dp"
        android:layout_marginTop="15dp"/>
</LinearLayout>

2.4 Button

  • 常用的功能有:

    1. 自定义背景形状
    2. 自定义按压效果
    3. 点击事件
  • 效果图
    在这里插入图片描述

2.4.1 主要部分代码实现(activity_button.xml)
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="10dp"
    >

    <!-- 文字大小、颜色 -->
    <Button
        android:id="@+id/btn_1"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:text="按钮1"
        android:background="#FF3300"
        android:textSize="20sp"
        android:textColor="#FFFFFF"
        />

    <!-- 自定义背景形状 -->
    <Button
        android:id="@+id/btn_2"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:layout_below="@id/btn_1"
        android:text="按钮2"
        android:textSize="20sp"
        android:textColor="#FFFFFF"
        android:background="@drawable/bg_btn2"
        android:layout_marginTop="10dp"/>

    <Button
        android:id="@+id/btn_3"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:layout_below="@id/btn_2"
        android:text="按钮3"
        android:textSize="20sp"
        android:textColor="#FF9900"
        android:background="@drawable/bg_btn3"
        android:layout_marginTop="10dp"/>

    <!-- 自定义按压效果 -->
    <Button
        android:id="@+id/btn_4"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:layout_below="@id/btn_3"
        android:text="按钮4"
        android:textSize="20sp"
        android:textColor="#FFFFFF"
        android:onClick="showToast"
        android:background="@drawable/bg_btn4"
        android:layout_marginTop="10dp"/>

    <!-- 点击事件 -->
    <TextView
        android:id="@+id/textview_1"
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:layout_below="@id/btn_4"
        android:layout_marginTop="10dp"
        android:background="#3C8DC4"
        android:text="文字"
        android:textSize="28sp"
        android:gravity="center"
        android:textColor="#FFFFFF"
        />
</RelativeLayout>

2.4.2 drawable中的自定义背景形状设计
  • shape的基本属性

    1. corners:定义圆角
    2. gradient:定义渐变色,可以定义两色渐变和三色渐变,及渐变样式
    3. solid:填充内部颜色
    4. stroke:描边属性,可以定义描边的宽度,颜色,虚实线等
  • 按钮2的圆角填充实现:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">

    <solid android:color="#FF9900"/>

    <!-- 设置矩形边拐角度 -->
    <corners android:radius="10dp"/>

</shape>
  • 按钮3描边实现:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">

    <stroke
        android:width="1dp"
        android:color="#FF9900"
        />

    <corners android:radius="10dp"/>

</shape>
  • 按钮4按压效果实现:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true">
        <shape>
            <solid android:color="#8AB34D"/>
            <corners android:radius="10dp"/>
        </shape>
    </item>

    <item android:state_pressed="false">
        <shape>
            <solid android:color="#F79709"/>
            <corners android:radius="10dp"/>
        </shape>
    </item>

</selector>
  • 点击事件实现
public class ButtonActivity extends AppCompatActivity {
    private Button mBtn3;
    private TextView mTv1;

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

        mBtn3 = (Button) findViewById(R.id.btn_3);
        mBtn3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(ButtonActivity.this,"btn3被点击了",Toast.LENGTH_SHORT).show();  //short是1秒钟,long是2秒钟
            }
        });

        mTv1 = (TextView) findViewById(R.id.textview_1);
        mTv1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(ButtonActivity.this,"tv_1被点击了",Toast.LENGTH_SHORT).show();
            }
        });
    }

    public void showToast(View v) {
        Toast.makeText(this,"btn4被点击了",Toast.LENGTH_SHORT).show();
    }
}

2.5 EditText以及登录页面制作

EditText就是用户可以进行点击输入信息的属性

  • 主要属性:
    • 1.hint:用于显示提示信息
    • 2.inputType:输入信息的格式(number就是只能输入数字,textPassword输入不可见密码形式)
2.5.1 登录页面制作

页面效果:
在这里插入图片描述- 页面代码实现:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="15dp"
    >

    <EditText
        android:id="@+id/et_1"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:hint="用户名"
        android:textSize="16sp"
        android:maxLines="1"
        android:textColor="#F79709"
        android:paddingLeft="10dp"
        android:paddingRight="10dp"
        android:background="@drawable/bg_login"
        android:drawableLeft="@drawable/icon_username"
        android:drawablePadding="5dp"
        android:layout_marginTop="50dp"
        />

    <EditText
        android:id="@+id/et_2"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:hint="密码"
        android:inputType="textPassword"
        android:textSize="16sp"
        android:maxLines="1"
        android:textColor="#F79709"
        android:paddingLeft="10dp"
        android:paddingRight="10dp"
        android:background="@drawable/bg_login"
        android:drawableLeft="@drawable/icon_password"
        android:drawablePadding="5dp"
        android:layout_below="@id/et_1"
        android:layout_marginTop="15dp"
        />

    <Button
        android:id="@+id/btn_login"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:text="登录"
        android:textSize="20sp"
        android:textColor="#FFFFFF"
        android:background="@drawable/bg_login_press"
        android:layout_below="@id/et_2"
        android:layout_marginTop="20dp"
        />

</RelativeLayout>

2.5.2 页面输入监听
public class EditTextActivity extends AppCompatActivity {
    private Button mbtnLogin;
    private EditText mEtUserName;

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

        mbtnLogin = (Button) findViewById(R.id.btn_login);
        mbtnLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(EditTextActivity.this,"登录成功!",Toast.LENGTH_SHORT).show();
            }
        });

//        进行用户名输入监听
        mEtUserName = (EditText) findViewById(R.id.et_1);
        mEtUserName.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            //输入监听
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                Log.d("editText",s.toString());
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
    }
}

2.6 RadioButton

主要作用:进行设计选择点击

页面效果
在这里插入图片描述- 常用属性以及自定义样式

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="15dp"
    >

    <RadioGroup
        android:id="@+id/rg_1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <RadioButton
            android:id="@+id/rb_1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="男"
            android:textSize="18sp"
            android:textColor="#FFA722"
            android:checked="true"
            />

        <RadioButton
            android:id="@+id/rb_2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="女"
            android:textSize="18sp"
            android:textColor="#FFA722"
            />
    </RadioGroup>

    <RadioGroup
        android:id="@+id/rg_2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_below="@id/rg_1"
        android:layout_marginTop="50dp">

        <RadioButton
            android:id="@+id/rb_3"
            android:layout_width="60dp"
            android:layout_height="40dp"
            android:text="男"
            android:gravity="center"
            android:checked="true"
            android:textSize="18sp"
            android:background="@drawable/bg_selector_radiobutton"
            android:button="@null"
            />

        <RadioButton
            android:id="@+id/rb_4"
            android:layout_width="60dp"
            android:layout_height="40dp"
            android:text="女"
            android:textSize="18sp"
            android:background="@drawable/bg_selector_radiobutton"
            android:button="@null"
            android:gravity="center"
            android:layout_marginLeft="15dp"
            />
    </RadioGroup>

</RelativeLayout>

2.7 CheckBox复选框

  • 页面效果图
    在这里插入图片描述- 实现代码
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="15dp">

    <TextView
        android:id="@+id/cb_title1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="你会哪些移动开发:"
        android:textSize="20sp"
        android:textColor="#000"/>

    <!-- 使用默认状态下的checkBox -->
    <CheckBox
        android:id="@+id/cb_1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/cb_title1"
        android:text="Android"
        android:textSize="20sp"
        android:layout_marginTop="10dp"
        />
    <CheckBox
        android:id="@+id/cb_2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/cb_1"
        android:text="iOS"
        android:textSize="20sp"
        android:layout_marginTop="5dp"
        />
    <CheckBox
        android:id="@+id/cb_3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/cb_2"
        android:text="H5"
        android:textSize="20sp"
        android:layout_marginTop="5dp"
        />
    <CheckBox
        android:id="@+id/cb_4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/cb_3"
        android:text="其他"
        android:textSize="20sp"
        android:layout_marginTop="5dp"
        />

    <!-- 自定义checkBox样式 -->
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_below="@+id/cb_4"
        >

        <TextView
            android:id="@+id/cb_title2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="你的爱好:"
            android:textSize="20sp"
            android:textColor="#000"
            android:layout_marginTop="20dp"
            />

        <CheckBox
            android:id="@+id/cb_5"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="编程"
            android:button="@drawable/bg_checkbox"
            android:paddingLeft="10dp"
            android:textSize="20sp"
            android:textColor="#000"
            android:layout_marginTop="10dp"
            />

        <CheckBox
            android:id="@+id/cb_6"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="其他"
            android:button="@drawable/bg_checkbox"
            android:paddingLeft="10dp"
            android:textSize="20sp"
            android:textColor="#000"
            android:layout_marginTop="10dp"
            />
    </LinearLayout>

</RelativeLayout>

  • 自定义CheckBox背景样式设计
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_checked="true" android:drawable="@drawable/icon_checkbox_true"/>

    <item android:state_checked="false" android:drawable="@drawable/icon_checkbox_false"/>
</selector>

2.8 ImageView以及使用第三方库加载资源(glide)

  • ImageView的主要属性:src、scaleType
    • 1.src:从drawable中选中你所想要的资源
    • 2.scale:
      • 2.1 fitXY:撑满控件,宽高比可能发生改变
      • 2.2 fitCenter:保持宽高比缩放,直至能够完全显示
      • 2.3 centerCrop:保持宽高比缩放,直至完全覆盖控件,裁剪显示

效果图:
在这里插入图片描述

2.8.1 代码实现
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="15dp">

    <!-- ImageView主要记住src、scaleType属性
        其中scaleType属性中几个重要的值:
            fitXY:撑满控件,宽高比可能发生改变
            fitCenter:保持宽高比缩放,直至能够完全显示
            centerCrop:保持宽高比缩放,直至完全覆盖控件,裁剪显示
     -->
    <ImageView
        android:id="@+id/iv_1"
        android:layout_width="200dp"
        android:layout_height="100dp"
        android:background="#FF7A22"
        android:src="@drawable/imageview"
        android:scaleType="fitXY"
        />

    <ImageView
        android:id="@+id/iv_2"
        android:layout_width="200dp"
        android:layout_height="100dp"
        android:layout_below="@id/iv_1"
        android:background="#FF7A22"
        android:src="@drawable/imageview"
        android:scaleType="fitCenter"
        android:layout_marginTop="10dp"
        />

    <ImageView
        android:id="@+id/iv_3"
        android:layout_width="200dp"
        android:layout_height="100dp"
        android:layout_below="@id/iv_2"
        android:layout_marginTop="10dp"
        android:background="#FF7A22"
        android:src="@drawable/imageview"
        android:scaleType="centerCrop"
        />

    <ImageView
        android:id="@+id/iv_4"
        android:layout_width="200dp"
        android:layout_height="100dp"
        android:layout_below="@id/iv_3"
        android:layout_marginTop="10dp"
        android:background="#FF7A22"
        android:scaleType="centerCrop"
        />

</RelativeLayout>

注意:其中上面的第四张图是通过加载网络图片而显示的(@+id/iv_4)

2.8.2 使用第三方库加载资源(glide)的步骤
  • 1.在AndroidManifest.xml中开启网络权限
    在这里插入图片描述- 2.代码示例
public class ImageViewActivity extends AppCompatActivity {
    private ImageView iMage;

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

        iMage = (ImageView) findViewById(R.id.iv_4);
        Glide.with(this).load("https://ss1.bdstatic.com/70cFvXSh_Q1YnxGkpoWK1HF6hhy/it/u=3120161399,505234538&fm=26&gp=0.jpg").into(iMage);
    }
}

2.9 ListView、GridView

2.9.1 ListView
  • 步骤一:创建好ListViewActivity后进行xml布局
    在这里插入图片描述
  • 步骤二:新建一个Adapter适配器MyListAdapter(继承BaseAdapter)
public class MyListAdapter extends BaseAdapter {

    private Context mContext;
    private LayoutInflater mLayoutInflater;

    public MyListAdapter(Context context) {
        this.mContext = context;
        mLayoutInflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return 10;
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    static class ViewHolder {
        public ImageView imageView;
        public TextView tvTitle,tvTime,tvContent;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if(convertView == null) {
            convertView = mLayoutInflater.inflate(R.layout.layout_list_item,null);
            holder = new ViewHolder();
            holder.imageView = (ImageView) convertView.findViewById(R.id.iv);
            holder.tvTitle = (TextView) convertView.findViewById(R.id.tv_title);
            holder.tvTime = (TextView) convertView.findViewById(R.id.tv_time);
            holder.tvContent = (TextView) convertView.findViewById(R.id.tv_content);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        //给控件赋值
        holder.tvTitle.setText("盲僧");
        holder.tvTime.setText("2020-3-1");
        holder.tvContent.setText("我用双手成就你的梦想");
        Glide.with(mContext).load("https://ss1.bdstatic.com/70cFvXSh_Q1YnxGkpoWK1HF6hhy/it/u=3120161399,505234538&fm=26&gp=0.jpg").into(holder.imageView);
        return convertView;
    }
}
  • 步骤三:在layout文件下面新建layout_list_item文件(也就是对上面MyLIstAdapter的单个convertView进行布局设置)
    在这里插入图片描述配置如下:
    在这里插入图片描述* 步骤四:设置每个convertView点击监听事件
public class ListViewActivity extends AppCompatActivity {
    private ListView mLv1;

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

        mLv1 = (ListView) findViewById(R.id.lv_1);
        mLv1.setAdapter(new MyListAdapter(ListViewActivity.this));

        //点击事件监听
        mLv1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Toast.makeText(ListViewActivity.this,"点击 pos "+position,Toast.LENGTH_SHORT).show();

                //或者实现页面点击跳转(略)
            }
        });

        //长按事件监听
        mLv1.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
                Toast.makeText(ListViewActivity.this,"长按 pos "+position,Toast.LENGTH_SHORT).show();
                return true;
            }
        });
    }

  • 步骤五:设置点击效果(listSelector属性设置自定义样式)
    在这里插入图片描述在drawable中新建自定义样式
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true" android:drawable="@color/colorOrange"/>
    <item android:state_selected="true" android:drawable="@color/colorOrange"/>
<!--    <item android:state_focused="true" android:drawable="@color/colorOrange"/>-->


<!--   不写默认就是false属性  -->
    <item  android:drawable="@color/colorTransparent"  />
</selector>
  • 效果图:
    在这里插入图片描述
2.9.2 GridView

注释:GridView和ListView的用法差不多,类比即可

部分区别:
在这里插入图片描述

2.10 ScrollView、HorizontalScrollView

2.10.1 ScrollView

适用于:当屏幕填充不下时,使用ScrollView可使得屏幕上下滚动

注意: 使用ScrollView时内置布局只能时单个的,不能有并列布局
例如:
ScrollView中内置的是单个LinearLayout布局,然后LinearLayout里面再可以进行并列使用多个Button
在这里插入图片描述

2.10.2 HorizontalScrollView

适用于:当屏幕左右填充不下时,使用HorizontalScrollView可使得屏幕左右滚动

注意: HorizontalScrollView与ScrollView的用法类似,使用HorizontalScrollView时内置布局只能时单个的,不能有并列布局

例如:
在这里插入图片描述

  • 代码实现:
<HorizontalScrollView
            android:layout_width="match_parent"
            android:layout_height="match_parent">

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

                <Button
                    android:id="@+id/btn_text3"
                    android:layout_width="200dp"
                    android:layout_height="wrap_content"
                    android:text="test3"
                    android:textAllCaps="false"
                    />
                <Button
                    android:id="@+id/btn_text4"
                    android:layout_width="200dp"
                    android:layout_height="wrap_content"
                    android:text="test4"
                    android:textAllCaps="false"
                    />
                <Button
                    android:id="@+id/btn_text5"
                    android:layout_width="200dp"
                    android:layout_height="wrap_content"
                    android:text="test5"
                    android:textAllCaps="false"
                    />

            </LinearLayout>
        </HorizontalScrollView>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值