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界面状态的保存代码详解的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: android 上下扫描动画,Andro
- 下一篇: android sina oauth2.