经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 移动开发 » Android » 查看文章
Android Studio屏幕方向以及UI界面状态的保存代码详解
来源:jb51  时间:2019/11/1 8:32:53  对本文有异议

项目:Orientation

  1. package com.example.orientation;
  2. import android.os.Bundle;
  3. import android.util.Log;
  4. import android.view.View;
  5. import android.widget.Button;
  6. import android.widget.TextView;
  7. import androidx.appcompat.app.AppCompatActivity;
  8. public class MainActivity extends AppCompatActivity {
  9. /*
  10. = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
  11. 本实例主要学习,屏幕翻转时,界面如何自适应,创建横屏布局
  12. 1.禁止切换横屏:在 AndroidManifest.xml-->application->activity->中设置如下代码(android:screenOrientation="portrait")
  13. <activity android:name=".MainActivity" android:screenOrientation="portrait" >
  14. 2. 创建 Landscape 布局,横屏时,会自动加载 Landscape 的布局界面(清单文件中,注意去掉 android:screenOrientation="portrait" )
  15. 3. 翻转屏幕时,保存窗口控件的状态值;
  16. = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
  17. */
  18. Button button;
  19. TextView textView;
  20. String TAG = "myTag";
  21. @Override
  22. protected void onCreate(Bundle savedInstanceState) {
  23. super.onCreate(savedInstanceState);
  24. setContentView(R.layout.activity_main);
  25. button = findViewById(R.id.button );
  26. textView = findViewById(R.id.textView);
  27. //如果State中的值不为空,如果有相应的这个组件的值,则读取出来赋值上去
  28. if(savedInstanceState !=null)
  29. {
  30. String s = savedInstanceState.getString("key");
  31. textView.setText(s);
  32. }
  33. button.setOnClickListener(new View.OnClickListener() {
  34. @Override
  35. public void onClick(View view) {
  36. textView.setText(button.getText());
  37. }
  38. });
  39. }
  40. @Override
  41. protected void onDestroy() {
  42. super.onDestroy();
  43. Log.d(TAG,"onDestroy:");
  44. }
  45. @Override
  46. //将 textView 中的值,先保存到 outState 中(键值对)
  47. public void onSaveInstanceState(Bundle outState) {
  48. super.onSaveInstanceState(outState);
  49. outState.putString("key",textView.getText().toString());
  50. }
  51. }

扩展学习:

UI界面设计

TextView

  1. <TextView
  2. android:id="@+id/textview"
  3. android:layout_width="match_parent"
  4. android:layout_height="wrap_content"
  5. android:gravity="center"
  6. android:text="This is a TextView"
  7. android:textColor="#00ff00"
  8. android:textSize="24sp" />

要想使得文字居中,需要添加属性android:gravity="center",可选择的选项还有top、bottom、left、right、center等,center相当于center_vertical|center_horizontal。
使用android:textSize="24sp"指定文字大小,android:textColor="#00ff00"指定文字颜色。

Button

  1. <Button
  2. android:id="@+id/button"
  3. android:layout_width="match_parent"
  4. android:layout_height="wrap_content"
  5. android:text="Button"
  6. android:textAllCaps="false"/>

在Android中,Button上面的文字默认英文全部大写,可以通过设置android:textAllCaps="false"改变

EditText

  1. <EditText
  2. android:id="@+id/edittext"
  3. android:layout_width="match_parent"
  4. android:layout_height="wrap_content"
  5. android:hint="HelloWorld"
  6. android:maxLength="20"
  7. android:maxLines="1" />

通过设置hint属性可以得到提示文字,设置maxLines使得输入框中最大输入行数。

以上相关知识点如果还有什么疏漏大家可以直接联系小编,感谢你的阅读和对w3xue的支持。

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号