安卓开发入门,基础语法介绍
程序员文章站
2022-03-23 23:01:27
安卓b站学习笔记梳理2020.11.27天哥在奔跑安卓教程b站网址https://www.bilibili.com/video/BV1Rt411e769.ImageView①Button的其他衍生控件:ToggleButton、Switch②常用属性③加载网络图片...
安卓b站学习笔记梳理2020.11.27
天哥在奔跑安卓教程b站网址https://www.bilibili.com/video/BV1Rt411e76
目录
①Button的其他衍生控件:ToggleButton、Switch
②常用属性(activity_listview.xml文件里的)
③Adapter接口(跟ListViewActivity同在listView文件夹下的MyListAdapter.java文件内容如下)
④R.layout.layout_list_item(每个item的内容布局,在MyListAdapter.java中convertView有使用)
⑤list_item.xml(在activity_listview.xml中的中用drawable)
9.ImageView
①Button的其他衍生控件:ToggleButton、Switch
自行百度
②常用属性
<ImageView
android:id="@+id/iv_1"
android:layout_width="200dp"
android:layout_height="120dp"
android:background="#FF9900"
android:src="@drawable/png1"
android:scaleType="fitXY"
/>
注意scaleType:
- fitXY:撑满控件,宽高比可能发生变化
- fitCenter:保持宽高比缩放,直至能够完全显示
- centerCrop:保持宽高比缩放,直至完全覆盖控件,裁剪显示
③加载网络图片
- 使用第三方glide库https://github.com/bumptech/glide(里面有各种文档)
use Gradle:
repositories { google() jcenter() } dependencies { implementation 'com.github.bumptech.glide:glide:4.11.0' annotationProcessor 'com.github.bumptech.glide:compiler:4.11.0' }
需要做的事情:在build.gradle文件中加入上面repositories的代码,并且在dependencies下面加入上面denpendencied的两行代码
- 在AndroidManifest.xml文件中加入以下代码表示可连接网络的权限
<uses-permission android:name="android.permission.INTERNET"/>
- ImageViewActivity.java中引用代码如下(前提是xml里面先有一个ImageView)
public class ImageViewActivity extends AppCompatActivity {
private ImageView mIv3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_image_view);
mIv3 = findViewById(R.id.iv_3);
Glide.with(this).load("https://tb2.bdstatic.com/tb/static-common/img/search_logo_big_v1_8d039f9.png").into(mIv3);
}
}
10.列表视图ListView(快被淘汰了)
①ListViewActivity.java
package com.jz.application1.listView;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ListView;
import androidx.annotation.Nullable;
import com.jz.application1.R;
public class ListViewActivity extends Activity {
private ListView mLv1;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_listview);
mLv1 = findViewById(R.id.lv_1);
mLv1.setAdapter(new MyListAdapter(ListViewActivity.this));
}
}
②常用属性(activity_listview.xml文件里的)
<ListView
android:id="@+id/lv_1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:listSelector="@drawable/list_item"/>
③Adapter接口(跟ListViewActivity同在listView文件夹下的MyListAdapter.java文件内容如下)
package com.jz.application1.listView;
import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.bumptech.glide.Glide;
import com.jz.application1.R;
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;
}
@SuppressLint({"SetTextI18n", "InflateParams"})//根据警报加的
@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 = convertView.findViewById(R.id.iv);
holder.tvTitle = convertView.findViewById(R.id.tv_title1);
holder.tvTime = convertView.findViewById(R.id.tv_time);
holder.tvContent = convertView.findViewById(R.id.tv_content);
convertView.setTag(holder);
}else {
holder = (ViewHolder) convertView.getTag();
}
//apply values to controller
holder.tvTitle.setText("This is title");
holder.tvTime.setText("2088-08-08");
holder.tvContent.setText("This is content!");
Glide.with(mContext).load("https://tb2.bdstatic.com/tb/static-common/img/search_logo_big_v1_8d039f9.png").into(holder.imageView);
return convertView;
}
}
④R.layout.layout_list_item(每个item的内容布局,在MyListAdapter.java中convertView有使用)
使用方式:
convertView = mLayoutInflater.inflate(R.layout.layout_list_item,null);
<?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:layout_height="match_parent"
android:paddingLeft="15dp"
android:paddingRight="15dp"
android:paddingTop="10dp"
android:paddingBottom="10dp">
<ImageView
android:id="@+id/iv"
android:layout_width="100dp"
android:layout_height="100dp"
android:scaleType="fitCenter"
android:background="#000000"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingLeft="10dp">
<TextView
android:id="@+id/tv_title1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="hello"
android:textSize="20sp"
android:textColor="@color/colorBlack"/>
<TextView
android:id="@+id/tv_time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="28sp"
android:text="2020-11-27"
android:textColor="@color/colorGrayDark"/>
<TextView
android:id="@+id/tv_content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:textColor="@color/colorGrayDark"
android:textSize="18sp"
android:text="This is content"/>
</LinearLayout>
</LinearLayout>
⑤list_item.xml(在activity_listview.xml中的<ListView>中用drawable)
使用方式:
android:listSelector="@drawable/list_item"
实现效果:点击会有颜色变化
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true"
android:drawable="@color/colorOrange"/>
<item android:state_pressed="true"
android:drawable="@color/colorGrayDark"/>
<item android:state_focused="true"
android:drawable="@color/colorAccent"/>
<item
android:drawable="@color/colorPrimary"/>
</selector>
本文地址:https://blog.csdn.net/weixin_42898615/article/details/110239497
上一篇: javascript有哪几种事件
下一篇: Python 文件管理实例详解