Android Studio屏幕方向以及UI界面狀態(tài)的保存代碼詳解
項(xiàng)目: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 { /* = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = 本實(shí)例主要學(xué)習(xí),屏幕翻轉(zhuǎn)時(shí),界面如何自適應(yīng),創(chuàng)建橫屏布局 1.禁止切換橫屏:在 AndroidManifest.xml-->application->activity->中設(shè)置如下代碼(android:screenOrientation="portrait") <activity android:name=".MainActivity" android:screenOrientation="portrait" > 2. 創(chuàng)建 Landscape 布局,橫屏?xí)r,會(huì)自動(dòng)加載 Landscape 的布局界面(清單文件中,注意去掉 android:screenOrientation="portrait" ) 3. 翻轉(zhuǎn)屏幕時(shí),保存窗口控件的狀態(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中的值不為空,如果有相應(yīng)的這個(gè)組件的值,則讀取出來(lái)賦值上去 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 中(鍵值對(duì)) public void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); outState.putString("key",textView.getText().toString()); } }
擴(kuò)展學(xué)習(xí):
UI界面設(shè)計(jì)
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",可選擇的選項(xiàng)還有top、bottom、left、right、center等,center相當(dāng)于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上面的文字默認(rèn)英文全部大寫(xiě),可以通過(guò)設(shè)置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" />
通過(guò)設(shè)置hint屬性可以得到提示文字,設(shè)置maxLines使得輸入框中最大輸入行數(shù)。
以上相關(guān)知識(shí)點(diǎn)如果還有什么疏漏大家可以直接聯(lián)系小編,感謝你的閱讀和對(duì)我們的支持。
欄 目:Android
下一篇:Android性能測(cè)試關(guān)注的指標(biāo)整理
本文標(biāo)題:Android Studio屏幕方向以及UI界面狀態(tài)的保存代碼詳解
本文地址:http://mengdiqiu.com.cn/a1/Android/9121.html
您可能感興趣的文章
- 01-10Android自定義View之繪制圓形頭像功能
- 01-10Android實(shí)現(xiàn)雙擊返回鍵退出應(yīng)用實(shí)現(xiàn)方法詳解
- 01-10android實(shí)現(xiàn)記住用戶名和密碼以及自動(dòng)登錄
- 01-10android實(shí)現(xiàn)簡(jiǎn)單計(jì)算器功能
- 01-10Android 友盟第三方登錄與分享的實(shí)現(xiàn)代碼
- 01-10android實(shí)現(xiàn)指紋識(shí)別功能
- 01-10Emoji表情在Android JNI中的兼容性問(wèn)題詳解
- 01-10Android實(shí)現(xiàn)圓形漸變加載進(jìn)度條
- 01-10android開(kāi)發(fā)環(huán)境中SDK文件夾下的所需內(nèi)容詳解
- 01-10android異步消息機(jī)制 源碼層面徹底解析(1)


閱讀排行
- 1C語(yǔ)言 while語(yǔ)句的用法詳解
- 2java 實(shí)現(xiàn)簡(jiǎn)單圣誕樹(shù)的示例代碼(圣誕
- 3利用C語(yǔ)言實(shí)現(xiàn)“百馬百擔(dān)”問(wèn)題方法
- 4C語(yǔ)言中計(jì)算正弦的相關(guān)函數(shù)總結(jié)
- 5c語(yǔ)言計(jì)算三角形面積代碼
- 6什么是 WSH(腳本宿主)的詳細(xì)解釋
- 7C++ 中隨機(jī)函數(shù)random函數(shù)的使用方法
- 8正則表達(dá)式匹配各種特殊字符
- 9C語(yǔ)言十進(jìn)制轉(zhuǎn)二進(jìn)制代碼實(shí)例
- 10C語(yǔ)言查找數(shù)組里數(shù)字重復(fù)次數(shù)的方法
本欄相關(guān)
- 01-10Android自定義View之繪制圓形頭像功能
- 01-10Android實(shí)現(xiàn)雙擊返回鍵退出應(yīng)用實(shí)現(xiàn)方
- 01-10android實(shí)現(xiàn)簡(jiǎn)單計(jì)算器功能
- 01-10android實(shí)現(xiàn)記住用戶名和密碼以及自動(dòng)
- 01-10C++自定義API函數(shù)實(shí)現(xiàn)大數(shù)相乘算法
- 01-10Android 友盟第三方登錄與分享的實(shí)現(xiàn)代
- 01-10android實(shí)現(xiàn)指紋識(shí)別功能
- 01-10如何給Flutter界面切換實(shí)現(xiàn)點(diǎn)特效
- 01-10Android實(shí)現(xiàn)圓形漸變加載進(jìn)度條
- 01-10Emoji表情在Android JNI中的兼容性問(wèn)題詳
隨機(jī)閱讀
- 01-11Mac OSX 打開(kāi)原生自帶讀寫(xiě)NTFS功能(圖文
- 01-10使用C語(yǔ)言求解撲克牌的順子及n個(gè)骰子
- 08-05DEDE織夢(mèng)data目錄下的sessions文件夾有什
- 01-10SublimeText編譯C開(kāi)發(fā)環(huán)境設(shè)置
- 01-11ajax實(shí)現(xiàn)頁(yè)面的局部加載
- 08-05dedecms(織夢(mèng))副欄目數(shù)量限制代碼修改
- 04-02jquery與jsp,用jquery
- 08-05織夢(mèng)dedecms什么時(shí)候用欄目交叉功能?
- 01-10delphi制作wav文件的方法
- 01-10C#中split用法實(shí)例總結(jié)