欢迎访问 生活随笔!

凯发k8官方网

当前位置: 凯发k8官方网 > 运维知识 > android >内容正文

android

android 方向控制界面,android studio屏幕方向以及ui界面状态的保存代码详解 -凯发k8官方网

发布时间:2024/10/8 android 0 豆豆
凯发k8官方网 收集整理的这篇文章主要介绍了 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使得输入框中最大输入行数。

以上相关知识点如果还有什么疏漏大家可以直接联系小编,感谢你的阅读和对萬仟网的支持。

总结

以上是凯发k8官方网为你收集整理的android 方向控制界面,android studio屏幕方向以及ui界面状态的保存代码详解的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得凯发k8官方网网站内容还不错,欢迎将凯发k8官方网推荐给好友。

网站地图