雷火电竞-中国电竞赛事及体育赛事平台

歡迎來到入門教程網(wǎng)!

Android

當前位置:主頁 > 軟件編程 > Android >

Android Studio屏幕方向以及UI界面狀態(tài)的保存代碼詳解

來源:本站原創(chuàng)|時間:2020-01-10|欄目:Android|點擊:

項目: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 {
/*
  = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
  本實例主要學習,屏幕翻轉時,界面如何自適應,創(chuàng)建橫屏布局
  1.禁止切換橫屏:在 AndroidManifest.xml-->application->activity->中設置如下代碼(android:screenOrientation="portrait")
   <activity android:name=".MainActivity" android:screenOrientation="portrait" >
  2. 創(chuàng)建 Landscape 布局,橫屏時,會自動加載 Landscape 的布局界面(清單文件中,注意去掉 android:screenOrientation="portrait" )
  3. 翻轉屏幕時,保存窗口控件的狀態(tài)值;
 
  = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
 */
  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使得輸入框中最大輸入行數(shù)。

以上相關知識點如果還有什么疏漏大家可以直接聯(lián)系小編,感謝你的閱讀和對我們的支持。

上一篇:詳解Android4.4 RIL短信接收流程分析

欄    目:Android

下一篇:Android性能測試關注的指標整理

本文標題:Android Studio屏幕方向以及UI界面狀態(tài)的保存代碼詳解

本文地址:http://www.jygsgssxh.com/a1/Android/9121.html

網(wǎng)頁制作CMS教程網(wǎng)絡編程軟件編程腳本語言數(shù)據(jù)庫服務器

如果侵犯了您的權利,請與我們聯(lián)系,我們將在24小時內進行處理、任何非本站因素導致的法律后果,本站均不負任何責任。

聯(lián)系QQ:835971066 | 郵箱:835971066#qq.com(#換成@)

Copyright © 2002-2020 腳本教程網(wǎng) 版權所有