Android 学习笔记(二)

系列文章目录

Anroid 学习笔记(一)


一、UI开发

控件

1、TextView

<TextView
android:id="@+id/text_view"
	android:layout_width="wrap_content"
	android:layout_height="wrap_content"
	android:text="This is TextView"
	android:gravity="center"
	/>

android:gravity=“xxx”:可以设置文字的对齐方式,可选值有 top, bottom, right, left, center 等,中间可以用 | 分割。

android:textSize=“24sp”:文字大小使用sp作为单位
android:textColor="#00ff00":指定文字的颜色

2、Button

可以用 android:textAllCaps="false"禁用自动大写转换

3、EditText

<EditText
    android:id="@+id/edit_text"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="Type something here"
    android:maxLines="2"
    tools:ignore="Autofill,HardcodedText,TextFields" />
    />

其中tools:ignore 是为了解除警告

4、ImageView

<ImageView
android:layout_width="wrap_content"
	android:layout_height="wrap_content"
	android:id="@+id/imgae_view"
	android:src="@drawable/img_1"
	tools:ignore="ContentDescription" 
	/>

图片资源存放在res/drawable-xhdpi 目录下
图片显示:

imageView.setImageResource(R.drawable.img_2);

5、

为了代码更加清楚,可以用接口的方式重写activity:

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        button.setOnClickListener(this);
    }
    @Override
    public void onClick(View v) {
	//添加按钮点击时的逻辑
    }
}

6、ProgressBar

用于在用户界面上显示一个进度条,可以通过setVisibility() 方法来设置进度条的可见性,可传入View.VISIBLE、View.INVISIBLE、View.GONE 三种状态。getVisibility() 方法可以获得控件的状态。

View.GONE 表示不可见并且不占用屏幕空间。
View.INVISBLE 表示不可见但是占用屏幕空间,相当于透明。

if(progressBar.getVisibility()==View.GONE) {
	progressBar.setVisibility(View.VISIBLE);
} else {
	progressBar.setVisibility(View.GONE);
}

通过 style 属性可以指定水平进度条,并且设置100为最大值。

style="?android:attr/progressBarStyleHorizontal"
android:max="100"

可以通过 getProgress() 和 setProgress() 方法来获取和设定当前的进度

int progress = progressBar.getProgress();
progress = progress + 10;
progressBar.setProgress(progress);

7、AlertDialog

可以在当前的界面弹出一个对话框,置顶于所有界面元素之上,能够屏蔽其他控件的交互能力。

8、ProgressDialog

与AlertDialog类似,不同的是它可以显示一个进度条,让用户耐心等待。

二、布局

1. LinearLayout(线性布局)

android:gravity() : 用于指定文字在控件中的对齐方式。
android:layout_gravity() : 用于指定控件在布局中的对齐方式。
android:layout_weight : 使用比例的方式来指定控件的大小,代表权重。

<TextView
	android:id="@+id/input_message"
	android:layout_width="0dp"
	android:layout_height="wrap_content"
	android:layout_weight="1"
	android:hint="Type something"
	tools:ignore="HardcodedText" />
<Button
	android:id="@+id/send"
	android:layout_width="0dp"
	android:layout_height="wrap_content"
	android:text="Button 1"
	android:layout_weight="1"
	tools:ignore="ButtonStyle,HardcodedText"
	/>

此时 android:layout_width 不再起作用,此时 TextView 和 Button 在水平方向上平分。
宽度自适配,TextView 会自动填满剩下的空间。

<TextView
	android:layout_width="0dp"
	android:layout_height="wrap_content"
	android:layout_weight="1"
	/>
<Button
	android:layout_width="wrap_content"
	android:layout_height="wrap_content"
	/>

2.RelativeLayout(相对布局)

相对于父布局定位:
android:layout_alignParentTop=“true”
android:layout_alignParentLeft
android:layout_alignParentBottom
android:layout_alignParentRight
android:layout_centerInParent

相对于控件定位:
android:layout_above="@id/button"
android:layout_toLeftOf
android:layout_toRightOf
android:layout_below

data = pd.read_csv(
    'https://labfile.oss.aliyuncs.com/courses/1283/adult.data.csv')
print(data.head())

3. FrameLayout(帧布局)

应用场景少,所有控件会默认摆放在布局的左上角。

4. 百分比布局

三、自定义控件

引入布局

如下:

<include layout="@layout/title" />

隐藏系统自带标题栏

 ActionBar actionbar=getSupportActionBar();
        if(actionbar != null) {
            actionbar.hide();
        }

getSupportActionBar() 方法可以获取ActionBar实例

自定义控件

重新定义一个 TitleLayout 类,并且为两个按钮注册点击事件。

public class TitleLayout extends LinearLayout {
    public TitleLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        LayoutInflater.from(context).inflate(R.layout.title, this);
        Button titleBack = findViewById(R.id.title_back);
        Button titleEdit = findViewById(R.id.title_edit);
        titleBack.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                ((Activity) getContext()).finish();
            }
        });
        titleEdit.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(getContext(), "You clicked Edit button", Toast.LENGTH_SHORT).show();
            }
        });
    }
}

四、ListView

没看懂,等以后有时间或者用到的时候再看吧。。。。

总结

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
1. Android 的基本概念 Android 是一个开源的操作系统,主要用于移动设备,如智能手机、平板电脑等。它基于 Linux 内核,提供了丰富的应用程序框架和 API,支持多种开发语言,如 Java、C/C++、Kotlin 等。 Android 应用程序由多个组件组成,包括活动(Activity)、服务(Service)、广播接收器(Broadcast Receiver)和内容提供器(Content Provider)等。这些组件可以组合在一起,形成复杂的应用程序。 2. Android 应用程序开发 Android 应用程序开发主要使用 Java 编程语言和 Android SDK。开发工具包括 Android Studio、Eclipse 等。 Android 应用程序的结构包括布局文件、资源文件、Java 代码和清单文件等。布局文件用于定义应用程序的用户界面,资源文件包括图像、声音、样式、主题等,Java 代码实现应用程序的逻辑,清单文件描述应用程序的组件和权限等信息。 3. Android 应用程序的调试和测试 Android 应用程序的调试和测试可以使用 Android Studio 提供的调试工具,包括断点调试、日志记录等。还可以使用模拟器或真实设备进行测试。 4. Android 应用程序的发布 发布 Android 应用程序需要进行签名和打包操作,签名用于验证应用程序的身份和完整性,打包将应用程序打包成 APK 文件,可以上传到应用商店进行发布。 5. Android 应用程序的优化 Android 应用程序的优化包括优化布局、资源、代码和网络等方面,以提高应用程序的性能和用户体验。其中,布局优化包括使用布局最优化算法、使用自定义视图等;资源优化包括压缩资源、使用向量图形等;代码优化包括使用异步任务、使用缓存等;网络优化包括使用数据压缩、使用本地存储等。 6. Android 开发的挑战 Android 开发面临的挑战包括设备碎片化、安全问题、性能问题等。设备碎片化指的是不同设备的屏幕尺寸、分辨率、操作系统版本等不同,需要对应用程序进行适配;安全问题指的是应用程序需要保证用户数据的安全和隐私;性能问题指的是应用程序需要保证快速响应和流畅运行。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值