当前位置:   article > 正文

Android.基本用法学习笔记

Android.基本用法学习笔记

设置文本的内容

先在strings.xml声明变量

方法1.

方法2.

设置文本的大小

1.单位dp,大家可以去学一下有关的单位换算

2.

设置文本颜色

1.

2.

4.设置文本背景颜色

1.

2.

设置视图的宽高

与上级视图一致,也就是上一级有多宽就有多少

1.

2.

3.

4.

设置视图的间距

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:app="http://schemas.android.com/apk/res-auto"
  4. xmlns:tools="http://schemas.android.com/tools"
  5. android:layout_width="match_parent"
  6. android:layout_height="match_parent"
  7. tools:context=".MainActivity">
  8. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  9. xmlns:app="http://schemas.android.com/apk/res-auto"
  10. xmlns:tools="http://schemas.android.com/tools"
  11. android:layout_width="match_parent"
  12. android:layout_height="match_parent"
  13. android:background="@color/blue"
  14. android:layout_margin="50sp"
  15. android:padding="50dp"
  16. tools:context=".MainActivity">
  17. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  18. xmlns:app="http://schemas.android.com/apk/res-auto"
  19. xmlns:tools="http://schemas.android.com/tools"
  20. android:layout_width="match_parent"
  21. android:layout_height="match_parent"
  22. android:background="@color/red"
  23. android:layout_margin="50sp"
  24. tools:context=".MainActivity">
  25. </LinearLayout>
  26. </LinearLayout>
  27. <TextView
  28. android:id="@+id/tv"
  29. android:layout_width="wrap_content"
  30. android:layout_height="wrap_content"
  31. android:text="@string/hello"
  32. android:textSize="40dp"
  33. android:background="@color/red"
  34. app:layout_constraintBottom_toBottomOf="parent"
  35. app:layout_constraintEnd_toEndOf="parent"
  36. app:layout_constraintStart_toStartOf="parent"
  37. app:layout_constraintTop_toTopOf="parent" />
  38. <!-- <Button
  39. android:id="@+id/button1"
  40. android:layout_width="wrap_content"
  41. android:layout_height="wrap_content"
  42. android:text="跳转1"
  43. app:layout_constraintBottom_toTopOf="@id/button2"
  44. app:layout_constraintEnd_toEndOf="parent"
  45. app:layout_constraintHorizontal_bias="0.501"
  46. app:layout_constraintStart_toStartOf="parent"
  47. app:layout_constraintTop_toBottomOf="@id/tv" />
  48. <Button
  49. android:id="@+id/button2"
  50. android:layout_width="wrap_content"
  51. android:layout_height="wrap_content"
  52. android:text="跳转2"
  53. app:layout_constraintTop_toBottomOf="@id/button1"
  54. app:layout_constraintStart_toStartOf="parent"
  55. app:layout_constraintEnd_toEndOf="parent"
  56. app:layout_constraintBottom_toBottomOf="parent"
  57. />-->
  58. </LinearLayout>

设置视图的对齐方式

线性布局

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:app="http://schemas.android.com/apk/res-auto"
  4. xmlns:tools="http://schemas.android.com/tools"
  5. android:layout_width="match_parent"
  6. android:layout_height="match_parent"
  7. android:orientation="vertical"
  8. tools:context=".MainActivity">
  9. <LinearLayout
  10. android:layout_width="match_parent"
  11. android:layout_height="wrap_content"
  12. android:background="@color/blue"
  13. android:layout_weight="1"
  14. android:orientation="horizontal"
  15. tools:context=".MainActivity"
  16. >
  17. <TextView
  18. android:id="@+id/tv1"
  19. android:layout_width="wrap_content"
  20. android:layout_height="wrap_content"
  21. android:text="@string/hello"
  22. android:textSize="30dp"
  23. android:background="@color/white"
  24. <TextView
  25. android:id="@+id/tv2"
  26. android:layout_width="wrap_content"
  27. android:layout_height="wrap_content"
  28. android:text="@string/hello"
  29. android:textSize="30dp"
  30. android:background="@color/red"
  31. </LinearLayout>
  32. <LinearLayout
  33. android:layout_width="wrap_content"
  34. android:layout_height="wrap_content"
  35. android:background="@color/pink"
  36. android:orientation="vertical"
  37. tools:context=".MainActivity"
  38. android:layout_weight="1"
  39. >
  40. <TextView
  41. android:id="@+id/tv1"
  42. android:layout_width="wrap_content"
  43. android:layout_height="wrap_content"
  44. android:text="@string/hello"
  45. android:textSize="30dp"
  46. android:background="@color/green"
  47. <TextView
  48. android:id="@+id/tv2"
  49. android:layout_width="wrap_content"
  50. android:layout_height="wrap_content"
  51. android:text="@string/hello"
  52. android:textSize="30dp"
  53. android:background="@color/red"
  54. </LinearLayout>
  55. <!-- <Button
  56. android:id="@+id/button1"
  57. android:layout_width="wrap_content"
  58. android:layout_height="wrap_content"
  59. android:text="跳转1"
  60. app:layout_constraintBottom_toTopOf="@id/button2"
  61. app:layout_constraintEnd_toEndOf="parent"
  62. app:layout_constraintHorizontal_bias="0.501"
  63. app:layout_constraintStart_toStartOf="parent"
  64. app:layout_constraintTop_toBottomOf="@id/tv" />
  65. <Button
  66. android:id="@+id/button2"
  67. android:layout_width="wrap_content"
  68. android:layout_height="wrap_content"
  69. android:text="跳转2"
  70. app:layout_constraintTop_toBottomOf="@id/button1"
  71. app:layout_constraintStart_toStartOf="parent"
  72. app:layout_constraintEnd_toEndOf="parent"
  73. app:layout_constraintBottom_toBottomOf="parent"
  74. />-->
  75. </LinearLayout>

相对布局

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:app="http://schemas.android.com/apk/res-auto"
  4. xmlns:tools="http://schemas.android.com/tools"
  5. android:layout_width="match_parent"
  6. android:layout_height="match_parent"
  7. tools:context=".MainActivity">
  8. <TextView
  9. android:id="@+id/tv1"
  10. android:layout_width="wrap_content"
  11. android:layout_centerHorizontal="true"
  12. android:layout_centerVertical="true"
  13. android:layout_height="wrap_content"
  14. android:text="中心"
  15. android:textSize="40dp"
  16. android:background="@color/green"/>
  17. <TextView
  18. android:id="@+id/tv2"
  19. android:layout_width="wrap_content"
  20. android:layout_centerHorizontal="true"
  21. android:layout_alignParentTop="true"
  22. android:layout_height="wrap_content"
  23. android:text="中心正上方"
  24. android:textSize="40dp"
  25. android:background="@color/green"/>
  26. <TextView
  27. android:id="@+id/tv3"
  28. android:layout_width="wrap_content"
  29. android:layout_centerVertical="true"
  30. android:layout_alignParentRight="true"
  31. android:layout_height="wrap_content"
  32. android:text="右方"
  33. android:textSize="40dp"
  34. android:background="@color/green"/>
  35. <TextView
  36. android:id="@+id/tv4"
  37. android:layout_width="wrap_content"
  38. android:layout_alignRight="@id/tv3"
  39. android:layout_height="wrap_content"
  40. android:text="1"
  41. android:textSize="40dp"
  42. android:background="@color/green"/>
  43. </RelativeLayout>

网格布局

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:app="http://schemas.android.com/apk/res-auto"
  4. xmlns:tools="http://schemas.android.com/tools"
  5. android:layout_width="match_parent"
  6. android:layout_height="match_parent"
  7. android:columnCount="2"
  8. android:rowCount="2"
  9. tools:context=".MainActivity">
  10. <TextView
  11. android:id="@+id/tv4"
  12. android:layout_width="wrap_content"
  13. android:layout_columnWeight="1"
  14. android:layout_height="60dp"
  15. android:text="1"
  16. android:textSize="40dp"
  17. android:gravity="center"
  18. android:background="@color/green"/>
  19. <TextView
  20. android:id="@+id/tv3"
  21. android:layout_width="wrap_content"
  22. android:layout_columnWeight="1"
  23. android:layout_height="60dp"
  24. android:text="2"
  25. android:gravity="center"
  26. android:textSize="40dp"
  27. android:background="@color/green"/>
  28. <TextView
  29. android:id="@+id/tv2"
  30. android:layout_columnWeight="1"
  31. android:layout_height="60dp"
  32. android:text="3"
  33. android:gravity="center"
  34. android:textSize="40dp"
  35. android:background="@color/green"/>
  36. <TextView
  37. android:id="@+id/tv1"
  38. android:layout_columnWeight="1"
  39. android:layout_height="60dp"
  40. android:text="4"
  41. android:gravity="center"
  42. android:textSize="40dp"
  43. android:background="@color/green"/>
  44. </GridLayout>

滚动视图

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout
  3. xmlns:android="http://schemas.android.com/apk/res/android"
  4. xmlns:app="http://schemas.android.com/apk/res-auto"
  5. xmlns:tools="http://schemas.android.com/tools"
  6. android:layout_width="match_parent"
  7. android:layout_height="match_parent"
  8. android:orientation="vertical">
  9. <HorizontalScrollView
  10. android:layout_width="wrap_content"
  11. android:layout_height="200dp">
  12. <LinearLayout
  13. android:layout_width="wrap_content"
  14. android:layout_height="match_parent"
  15. android:orientation="horizontal">
  16. <View
  17. android:layout_width="300dp"
  18. android:layout_height="300dp"
  19. android:background="@color/green"/>
  20. <View
  21. android:layout_width="300dp"
  22. android:layout_height="300dp"
  23. android:background="@color/blue" />
  24. </LinearLayout>
  25. </HorizontalScrollView>
  26. </LinearLayout>

按钮控件Button

  

activity xml代码

  1. package com.example.test2;
  2. import androidx.appcompat.app.AppCompatActivity;
  3. import android.os.Bundle;
  4. import android.os.ParcelUuid;
  5. import android.view.View;
  6. import android.widget.Button;
  7. import android.widget.TextView;
  8. public class ButtonClickActivity extends AppCompatActivity {
  9. private TextView tv_result;
  10. @Override
  11. protected void onCreate(Bundle savedInstanceState) {
  12. super.onCreate(savedInstanceState);
  13. setContentView(R.layout.activity_button_click);
  14. // 找到指定的按钮
  15. tv_result = findViewById(R.id.tv_result);
  16. Button buttonclick1 = findViewById(R.id.buttonclick1);
  17. buttonclick1.setOnClickListener(new MyOnClickListener(tv_result));
  18. }
  19. static class MyOnClickListener implements View.OnClickListener {
  20. private final TextView tv_result;
  21. public MyOnClickListener(TextView tv_result) {
  22. this.tv_result = tv_result;
  23. }
  24. @Override
  25. public void onClick(View v) {
  26. String desc = String.format("%s 你点击的按钮是: %s", DateUtil.getNowTime(), ((Button)v).getText());
  27. tv_result.setText(desc);
  28. }
  29. }
  30. }

button click java代码

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:app="http://schemas.android.com/apk/res-auto"
  4. xmlns:tools="http://schemas.android.com/tools"
  5. android:layout_width="match_parent"
  6. android:layout_height="match_parent"
  7. tools:context=".ButtonClickActivity"
  8. android:orientation="vertical">
  9. <Button
  10. android:id="@+id/buttonclick1"
  11. android:layout_width="match_parent"
  12. android:layout_height="wrap_content"
  13. android:text="指定单独的点击监听器"
  14. android:textColor="@color/black"
  15. android:textSize="15sp"
  16. />
  17. <TextView
  18. android:id="@+id/tv_result"
  19. android:layout_width="match_parent"
  20. android:layout_height="wrap_content"
  21. android:padding="5dp"
  22. android:gravity="center"
  23. android:textColor="@color/black"
  24. android:textSize="15dp"
  25. android:text="点击查看结果"
  26. />
  27. </LinearLayout>

DateUtil代码

  1. package com.example.test2;
  2. import java.text.SimpleDateFormat;
  3. import java.util.Date;
  4. public class DateUtil {
  5. public static String getNowTime()
  6. {
  7. SimpleDateFormat sdf=new SimpleDateFormat("HH:mm:ss");
  8. return sdf.format(new Date());
  9. }
  10. }

Androidmainifest代码

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:tools="http://schemas.android.com/tools">
  4. <application
  5. android:allowBackup="true"
  6. android:dataExtractionRules="@xml/data_extraction_rules"
  7. android:fullBackupContent="@xml/backup_rules"
  8. android:icon="@mipmap/ic_launcher"
  9. android:label="@string/app_name"
  10. android:roundIcon="@mipmap/ic_launcher_round"
  11. android:supportsRtl="true"
  12. android:theme="@style/Theme.Test2"
  13. tools:targetApi="31">
  14. <activity
  15. android:name=".MainActivity3"
  16. android:exported="false" />
  17. <activity
  18. android:name=".ButtonClickActivity"
  19. android:exported="true">
  20. <intent-filter>
  21. <action android:name="android.intent.action.MAIN" />
  22. <category android:name="android.intent.category.LAUNCHER" />
  23. </intent-filter>
  24. </activity>
  25. <activity android:name=".MainActivity2" />
  26. </application>
  27. </manifest>

图像视图

1.

2.

声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号