欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

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

程序员文章站 2022-06-20 14:51:38
项目:orientation package com.example.orientation; import android.os.bundle; import a...

项目: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使得输入框中最大输入行数。

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