Android Studio屏幕方向以及UI界面狀態(tài)的保存代碼詳解
項目: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 { /* = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = 本實例主要學習,屏幕翻轉(zhuǎn)時,界面如何自適應,創(chuàng)建橫屏布局 1.禁止切換橫屏:在 AndroidManifest.xml-->application->activity->中設(shè)置如下代碼(android:screenOrientation="portrait") <activity android:name=".MainActivity" android:screenOrientation="portrait" > 2. 創(chuàng)建 Landscape 布局,橫屏時,會自動加載 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中的值不為空,如果有相應的這個組件的值,則讀取出來賦值上去 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界面設(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",可選擇的選項還有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上面的文字默認英文全部大寫,可以通過設(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)知識點如果還有什么疏漏大家可以直接聯(lián)系小編,感謝你的閱讀和對腳本之家的支持。
相關(guān)文章
Android應用的LinearLayout中嵌套RelativeLayout的布局用法
這篇文章主要介紹了Android應用的LinearLayout中嵌套RelativeLayout的布局用法,文后還給出了線性布局中一些組件位置的調(diào)試經(jīng)驗,需要的朋友可以參考下2016-04-04Golang+Android基于HttpURLConnection實現(xiàn)的文件上傳功能示例
這篇文章主要介紹了Golang+Android基于HttpURLConnection實現(xiàn)的文件上傳功能,結(jié)合具體實例形式分析了Android基于HttpURLConnection的客戶端結(jié)合Go語言服務(wù)器端實現(xiàn)文件上傳功能的操作技巧,需要的朋友可以參考下2017-03-03Android對話框AlertDialog與DatePickerDialog及TimePickerDialog使用詳解
這篇文章主要介紹了Android對話框中的提醒對話框AlertDialog、日期對話框DatePickerDialog、時間對話框TimePickerDialog使用方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習吧2022-09-09Android studio將Module打包成Jar的方法
這篇文章主要介紹了Android studio將Module打包成Jar的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-10-10