Android Studio屏幕方向以及UI界面状态的保存代码详解

 更新时间:2019年10月31日 16:03:20   作者:孤峰皓月  
在本篇文章里小编给各位整理的是关于Android Studio屏幕方向以及UI界面状态的保存代码以及相关知识点,需要的跟着学习下。

项目:Orientation

package com.example.orientation;
 
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
 
import androidx.appcompat.app.AppCompatActivity;
 
public class MainActivity extends AppCompatActivity {
/*
  = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
  本实例主要学习,屏幕翻转时,界面如何自适应,创建横屏布局
  1.禁止切换横屏:在 AndroidManifest.xml-->application->activity->中设置如下代码(android:screenOrientation="portrait")
   <activity android:name=".MainActivity" android:screenOrientation="portrait" >
  2. 创建 Landscape 布局,横屏时,会自动加载 Landscape 的布局界面(清单文件中,注意去掉 android:screenOrientation="portrait" )
  3. 翻转屏幕时,保存窗口控件的状态值;
 
  = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
 */
  Button button;
  TextView textView;
 
  String TAG = "myTag";
 
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
 
    button = findViewById(R.id.button );
    textView = findViewById(R.id.textView);
 
    //如果State中的值不为空,如果有相应的这个组件的值,则读取出来赋值上去
    if(savedInstanceState !=null)
    {
      String s = savedInstanceState.getString("key");
      textView.setText(s);
    }
 
    button.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View view) {
        textView.setText(button.getText());
      }
    });
  }
 
  @Override
  protected void onDestroy() {
    super.onDestroy();
    Log.d(TAG,"onDestroy:");
  }
 
  @Override
  //将 textView 中的值,先保存到 outState 中(键值对)
  public void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    outState.putString("key",textView.getText().toString());
  }
}

扩展学习:

UI界面设计

TextView

<TextView
    android:id="@+id/textview"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:text="This is a TextView"
    android:textColor="#00ff00"
    android:textSize="24sp" />

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

Button

<Button
    android:id="@+id/button"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Button"
    android:textAllCaps="false"/>

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

EditText

<EditText
    android:id="@+id/edittext"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="HelloWorld"
    android:maxLength="20"
    android:maxLines="1" />

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

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

相关文章

  • Android开发中优秀的app 异常处理机制

    Android开发中优秀的app 异常处理机制

    这篇文章主要介绍了Android开发中优秀的app 异常处理机制 的相关资料,需要的朋友可以参考下
    2015-12-12
  • 使用Retrofit下载文件并实现进度监听的示例

    使用Retrofit下载文件并实现进度监听的示例

    这篇文章主要介绍了使用Retrofit下载文件并实现进度监听的示例,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-08-08
  • RadioGroup实现单选框的多行排列

    RadioGroup实现单选框的多行排列

    这篇文章主要为大家详细介绍了RadioGroup实现单选框的多行排列,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2019-11-11
  • Android自定义Dialog的2种常见方法

    Android自定义Dialog的2种常见方法

    这篇文章主要分享了Android自定义Dialog的2种常见方法,大多数中,使用系统内置的dialog并不能满足UE的设计需要,关于两种方法下面文章将详细介绍,需要的小伙伴可以参考一下
    2022-05-05
  • AccessibilityService实现微信发红包功能

    AccessibilityService实现微信发红包功能

    这篇文章主要为大家详细介绍了AccessibilityService实现微信发红包功能,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2019-12-12
  • Android实现类似微信的文本输入框 效果

    Android实现类似微信的文本输入框 效果

    本文给大家介绍一下微信的文本输入框是如何实现的,其实那只是个普通的文本框设了一个特殊的背景而已。具体微信怎么实现的,大家可以反编译下,这里介绍下如何实现这个背景
    2017-05-05
  • AndroidStudio利用android-support-multidex解决64k的各种异常

    AndroidStudio利用android-support-multidex解决64k的各种异常

    这篇文章主要为大家详细介绍了AndroidStudio利用android-support-multidex解决64k的各种异常,感兴趣的小伙伴们可以参考一下
    2016-09-09
  • Android编程实现应用自动更新、下载、安装的方法

    Android编程实现应用自动更新、下载、安装的方法

    这篇文章主要介绍了Android编程实现应用自动更新、下载、安装的方法,涉及Android针对应用程序包的读取,属性判断与更新操作的相关技巧,需要的朋友可以参考下
    2016-02-02
  • Android Jetpack组件库LiveData源码深入探究

    Android Jetpack组件库LiveData源码深入探究

    LiveData是Jetpack组件的一部分,更多的时候是搭配ViewModel来使用,相对于Observable,LiveData的最大优势是其具有生命感知的,换句话说,LiveData可以保证只有在组件( Activity、Fragment、Service)处于活动生命周期状态的时候才会更新数据
    2022-09-09
  • Android使用 Coroutine + Retrofit打造简单的HTTP请求库

    Android使用 Coroutine + Retrofit打造简单的HTTP请求库

    这篇文章主要介绍了Android使用 Coroutine + Retrofit打造简单的HTTP请求库,帮助大家更好的理解和学习使用Android,感兴趣的朋友可以了解下
    2021-03-03

最新评论