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

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

概述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 M

项目: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 Studio屏幕方向以及UI界面状态的保存代码详解全部内容,希望文章能够帮你解决Android Studio屏幕方向以及UI界面状态的保存代码详解所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1144336.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-31
下一篇2022-05-31

发表评论

登录后才能评论

评论列表(0条)

    保存