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)時,界面如何自適應(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,會自動加載 Landscape 的布局界面(清單文件中,注意去掉 android:screenOrientation="portrait" )
3. 翻轉(zhuǎn)屏幕時,保存窗口控件的狀態(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)的這個組件的值,則讀取出來賦值上去
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());
}
}
擴(kuò)展學(xué)習(xí):
UI界面設(shè)計
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)英文全部大寫,可以通過設(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" />
通過設(shè)置hint屬性可以得到提示文字,設(shè)置maxLines使得輸入框中最大輸入行數(shù)。
以上相關(guān)知識點(diǎn)如果還有什么疏漏大家可以直接聯(lián)系小編,感謝你的閱讀和對腳本之家的支持。
相關(guān)文章
Android 創(chuàng)建與解析XML(四)——詳解Pull方式
本篇文章主要介紹了Android創(chuàng)建與解析XML(二)——詳解Pull方式,這里整理了詳細(xì)的代碼,有需要的小伙伴可以參考下。2016-11-11
Android Studio進(jìn)行APP圖標(biāo)更改的兩種方式總結(jié)
這篇文章主要介紹了Android Studio進(jìn)行APP圖標(biāo)更改的兩種方式總結(jié),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-06-06
Android Studio添加第三方庫的注意事項(xiàng)
這篇文章給大家介紹的是Android Studio添加第三方庫遇到的一些坑,以及對應(yīng)的解決辦法,有需要的可以參考借鑒。2016-09-09
Android布局控件DrawerLayout實(shí)現(xiàn)完美側(cè)滑效果
這篇文章主要為大家詳細(xì)介紹了Android布局控件DrawerLayout實(shí)現(xiàn)完美側(cè)滑效果,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-08-08
ionic App 解決android端在真機(jī)上tab處于頂部的問題
這篇文章主要介紹了ionic App 解決android端在真機(jī)上tab處于頂部的問題的相關(guān)資料,需要的朋友可以參考下2017-06-06
Android ListView實(shí)現(xiàn)無限循環(huán)滾動
這篇文章主要為大家詳細(xì)介紹了Android ListView實(shí)現(xiàn)無限循環(huán)滾動,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2020-06-06
android自定義ListView實(shí)現(xiàn)底部View自動隱藏和消失的功能
本篇文章主要介紹了android自定義ListView實(shí)現(xiàn)底部View自動隱藏和消失的功能 ,具有一定的參考價值,感興趣的小伙伴們可以參考一下。2017-03-03
Android開發(fā)listview選中高亮簡單實(shí)現(xiàn)代碼分享
這篇文章主要介紹了Android開發(fā)listview選中高亮簡單實(shí)現(xiàn)代碼分享,具有一定借鑒價值,需要的朋友可以參考下2018-01-01
android之HttpPost&HttpGet使用方法介紹
下文直接講用法,先知道怎么用,再知道怎么回事,具體如下,感興趣的朋友可以參考下哈2013-06-06

