当前位置: 首页 > 知识库问答 >
问题:

Android为定向设置布局的最简单方法

哈栋
2023-03-14

我在manifest中添加了android:configchanges=“orientationkeyboardhiddenscreensize”,并在res文件夹下创建了两个xml activity_main(端口)和layout(land)。当我旋转时,我想设置它在两个layout之间的变化,如何编辑我的代码?谢谢,在我改变了两次之后,oncreate下的方法不起作用了。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="fill_parent"
    android:layout_height="fill_parent"   android:paddingTop="5dp">
<TextView  android:id="@+id/tv" android:layout_width="fill_parent"
android:layout_height="40dip" android:layout_marginRight="5dip"
android:layout_marginLeft="5dip"  android:background="#FFFFFF"
android:gravity="center_vertical|right" android:textSize="30dip"
android:textColor="#ff0000"></TextView>

<LinearLayout

    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:paddingTop="8dip">

    <Button
        android:layout_width="80dip"
        android:layout_height="wrap_content"
        android:text="7"
        android:id="@+id/Button07"/>
    <Button
        android:layout_width="80dip"
        android:layout_height="wrap_content"
        android:text="8"
        android:id="@+id/Button08"/><Button
    android:layout_width="80dip"
    android:layout_height="wrap_content"
    android:text="9"
    android:id="@+id/Button09"/><Button
    android:layout_width="80dip"
    android:layout_height="wrap_content"
    android:text="+"
    android:id="@+id/ButtonJia"/>

    </LinearLayout>
<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:paddingTop="8dip">
    <Button
        android:layout_width="80dip"
        android:layout_height="wrap_content"
    android:text="4"
        android:id="@+id/Button04"></Button>
    <Button
        android:layout_width="80dip"
        android:layout_height="wrap_content"
        android:text="5"
        android:id="@+id/Button05"></Button><Button
    android:layout_width="80dip"
    android:layout_height="wrap_content"
    android:text="6"
    android:id="@+id/Button06"></Button><Button
    android:layout_width="80dip"
    android:layout_height="wrap_content"
    android:text="-"
    android:id="@+id/ButtonJian"></Button>

    </LinearLayout>
    <LinearLayout

        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:paddingTop="8dip">

        <Button
            android:layout_width="80dip"
            android:layout_height="wrap_content"
            android:text="1"
            android:id="@+id/Button01"/>
        <Button
            android:layout_width="80dip"
            android:layout_height="wrap_content"
            android:text="2"
            android:id="@+id/Button02"/><Button
        android:layout_width="80dip"
        android:layout_height="wrap_content"
        android:text="3"
        android:id="@+id/Button03"/><Button
        android:layout_width="80dip"
        android:layout_height="wrap_content"
        android:text="*"
        android:id="@+id/ButtonCheng"/>

    </LinearLayout>
    <LinearLayout

        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:paddingTop="8dip">


        <Button
            android:layout_width="80dip"
            android:layout_height="wrap_content"
            android:text="c"
            android:id="@+id/ButtonC"/>
        <Button
            android:layout_width="80dip"
            android:layout_height="wrap_content"
            android:text="0"
            android:id="@+id/Button00"/><Button
        android:layout_width="80dip"
        android:layout_height="wrap_content"
        android:text="="
        android:id="@+id/ButtonDengyu"/><Button
        android:layout_width="80dip"
        android:layout_height="wrap_content"
        android:text="/"
        android:id="@+id/ButtonChu"/>

    </LinearLayout>
    <TextView  android:id="@+id/tv1" android:layout_width="fill_parent"
        android:layout_height="40dip" android:layout_marginRight="5dip"
        android:layout_marginLeft="5dip"  android:background="#FFFFFF"
        android:gravity="center_vertical|right" android:textSize="30dip"
        android:textColor="#000000"></TextView>

    <LinearLayout

        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:paddingTop="8dip">

        <Button
            android:layout_width="80dip"
            android:layout_height="wrap_content"
            android:text="7"
            android:id="@+id/Button071"/>
        <Button
            android:layout_width="80dip"
            android:layout_height="wrap_content"
            android:text="8"
            android:id="@+id/Button081"/><Button
        android:layout_width="80dip"
        android:layout_height="wrap_content"
        android:text="9"
        android:id="@+id/Button091"/><Button
        android:layout_width="80dip"
        android:layout_height="wrap_content"
        android:text="+"
        android:id="@+id/ButtonJia1"/>

    </LinearLayout>
    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:paddingTop="8dip">
        <Button
            android:layout_width="80dip"
            android:layout_height="wrap_content"
            android:text="4"
            android:id="@+id/Button041"></Button>
        <Button
            android:layout_width="80dip"
            android:layout_height="wrap_content"
            android:text="5"
            android:id="@+id/Button051"></Button><Button
        android:layout_width="80dip"
        android:layout_height="wrap_content"
        android:text="6"
        android:id="@+id/Button061"></Button><Button
        android:layout_width="80dip"
        android:layout_height="wrap_content"
        android:text="-"
        android:id="@+id/ButtonJian1"></Button>

    </LinearLayout>
    <LinearLayout

        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:paddingTop="8dip">

        <Button
            android:layout_width="80dip"
            android:layout_height="wrap_content"
            android:text="1"
            android:id="@+id/Button011"/>
        <Button
            android:layout_width="80dip"
            android:layout_height="wrap_content"
            android:text="2"
            android:id="@+id/Button021"/><Button
        android:layout_width="80dip"
        android:layout_height="wrap_content"
        android:text="3"
        android:id="@+id/Button031"/><Button
        android:layout_width="80dip"
        android:layout_height="wrap_content"
        android:text="*"
        android:id="@+id/ButtonCheng1"/>

    </LinearLayout>
    <LinearLayout

        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:paddingTop="8dip">


        <Button
            android:layout_width="80dip"
            android:layout_height="wrap_content"
            android:text="c"
            android:id="@+id/ButtonC1"/>
        <Button
            android:layout_width="80dip"
            android:layout_height="wrap_content"
            android:text="0"
            android:id="@+id/Button001"/><Button
        android:layout_width="80dip"
        android:layout_height="wrap_content"
        android:text="="
        android:id="@+id/ButtonDengyu1"/><Button
        android:layout_width="80dip"
        android:layout_height="wrap_content"
        android:text="/"
        android:id="@+id/ButtonChu1"/>

    </LinearLayout>
</LinearLayout>

共有1个答案

姬浩渺
2023-03-14

如果我对您的理解是正确的,您需要在您的res目录中创建layout-land和layout-port文件夹,并在每个文件夹中创建具有相同名称的布局。例如,在layoutland文件夹中创建activitymain.xml布局,在layout-port文件夹中创建同名的不同布局。然后在活动中调用SetContentView(r.layout.activity_main)

 类似资料:
  • 本文向大家介绍Android简单实现自定义流式布局的方法,包括了Android简单实现自定义流式布局的方法的使用技巧和注意事项,需要的朋友参考一下 本文实例讲述了Android简单实现自定义流式布局的方法。分享给大家供大家参考,具体如下: 首先来看一下 手淘HD - 商品详情 - 选择商品属性 页面的UI 商品有很多尺码,而且展现每个尺码所需要的View的大小也不同(主要是宽度),所以在从服务器端

  • 问题内容: 我正在研究Android项目。我有一个prefs.xml代码,像这样 而且我需要自定义首选项布局。我创造了; custom_name_setting_layout.xml 并编写一个SettingActivity.java 我的问题是;我写了setBackgroundColor方法,但是没有用。不起作用的意思是,该程序正在运行而没有错误(例如NullReferenceException

  • 本文向大家介绍Android RecyclerView布局就这么简单,包括了Android RecyclerView布局就这么简单的使用技巧和注意事项,需要的朋友参考一下 RecyclerView是什么? 笔者个人看法,RecyclerView只是一个对ListView的升级版,这个升级的主要目的是为了让这个view的效率更高,并且使用更加方便。 我们知道,ListView通过使用ViewHold

  • 本文向大家介绍Android动态添加设置布局与控件的方法,包括了Android动态添加设置布局与控件的方法的使用技巧和注意事项,需要的朋友参考一下 本文实例讲述了Android动态添加设置布局与控件的方法。分享给大家供大家参考,具体如下: 有时候我们会在代码端,动态的设置,添加布局和控件。下面我们就看来看一下如何处理,直接上代码,代码里面的注解很清楚了。 布局文件:fragment_hot.xml

  • 我一直在使用空布局,很多人会说不应该这样做。有更好的办法吗? 以下是一些代码示例:

  • 本文向大家介绍Android设置Activity背景为透明style的简单方法(必看),包括了Android设置Activity背景为透明style的简单方法(必看)的使用技巧和注意事项,需要的朋友参考一下 方法一: 通过Theme.Translucent 只需要在Manifest中需要透明的Activity内设置theme为以上任意一个就可以了 方法二: 自定义style,就像自定义Dialog