日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > Android >内容正文

Android

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

發布時間:2024/10/8 Android 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 android 方向控制界面,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")

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

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

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

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 方向控制界面,Android Studio屏幕方向以及UI界面状态的保存代码详解的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。