Android相对布局正在屏幕外运行 [英] Android Relative Layout is running off screen

查看:74
本文介绍了Android相对布局正在屏幕外运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了一个问题,即相对布局似乎在模拟器中的屏幕外运行,但在设计器中正确显示.

I have a problem in which a relative layout seems to be running off the screen in the emulator but showing properly in the designer.

我在RelativeLayout中都有一个LinearLayout和一个按钮.LinearLayout包含一些TextView,然后是带有单选按钮(要动态分配的数字)的ScrollView.

I have a LinearLayout and a button both inside a RelativeLayout. The LinearLayout contains some TextViews and then a ScrollView with radio buttons (number to be dynamically allocated).

将LinearLayout放置在RelativeLayout中,然后将按钮放置在屏幕底部,而不考虑单选按钮计数.这使我可以将按钮锚定在屏幕底部,而将LinearLayout的底部锚定在按钮顶部.

Putting the LinearLayout and the button in the RelativeLayout gets the button to stay at the screen bottom regardless of radio button count. This allows me to anchor the button to the bottom of the screen and the bottom of the LinearLayout to the top of the button.

在Android Studio设计器中,效果很好!该按钮位于底部,如果收音机延伸到屏幕之外,您可以滚动它们.当我运行模拟器时,尽管在按钮之前屏幕被切断了!我已经在Microsoft和Android(如此缓慢)的模拟器中进行了尝试,并获得了相同的结果.

In the Android Studio designer this works great! The button is at the bottom, and if the radios extend beyond the screen you can scroll them. When I run the emulator though the screen gets cut off before the button! I've tried this in both the Microsoft and Android (so slow) emulators and gotten the same result.

我在 Android上发现了类似的问题:Android的相对布局内容低于屏幕指出了将这行添加到我的按钮中的位置:

I found a similar question at Android: Relative layout content goes below screen which pinpointed adding this line to my button:

android:layout_marginBottom="@dimen/activity_horizontal_margin"

这使它回到了屏幕的一半,但并没有完全消失,该线程的其余部分是关于删除我想在此用例中保留的LinearLayout.似乎这里正在发生更多事情.我究竟做错了什么?这是我的XML:

That brought it partway back on screen but not all the way, and the rest of that thread is about removing the LinearLayout which I want to keep in this use case. Seems something more is going on here. What am I doing wrong? Here's my XML:

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:paddingBottom="16dp"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.complexityresolved.lifetracker.RadioInputsActivity$PlaceholderFragment"
    android:gravity="center_horizontal">

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.complexityresolved.lifetracker.RadioInputsActivity$PlaceholderFragment"
    android:orientation="vertical"
    android:layout_centerHorizontal="true"
    android:layout_above="@+id/radioInputsActivityDoneButton">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="Text With Trait Name"
        android:id="@+id/RadioScreenTextViewTraitName"
        android:layout_gravity="center_horizontal"
        android:clickable="true" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:text="Text With Short Description"
        android:id="@+id/RadioScreenTextViewShortDescription"
        android:layout_gravity="center_horizontal"
        android:visibility="gone" />

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/scrollView">
        <RadioGroup
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
            <RadioButton android:id="@+id/radio_pirates"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="pirates"/>
            <RadioButton android:id="@+id/radio_ninjas"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="ninjas"/>
            <RadioButton android:id="@+id/radio_irates"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="pirates"/>
            <RadioButton android:id="@+id/radio_injas"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="ninjas"/>
            <RadioButton android:id="@+id/radio_rates"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="pirates"/>
            <RadioButton android:id="@+id/radio_njas"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="ninjas"/>
            <RadioButton android:id="@+id/radio_ates"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="pirates"/>
            <RadioButton android:id="@+id/radio_jas"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="ninjas"/>
            <RadioButton android:id="@+id/adio_pirates"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="pirates"/>
            <RadioButton android:id="@+id/adio_ninjas"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="ninjas"/>
            <RadioButton android:id="@+id/dio_pirates"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="pirates"/>
            <RadioButton android:id="@+id/dio_ninjas"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="ninjas"/>
            <RadioButton android:id="@+id/raio_pirates"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="pirates"/>
            <RadioButton android:id="@+id/raio_ninjas"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="ninjas"/>
            <RadioButton android:id="@+id/rao_pirates"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="pirates"/>
            <RadioButton android:id="@+id/rao_ninjas"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="ninjas"/>
        </RadioGroup>
    </ScrollView>

</LinearLayout>

    <Button
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/radioInputsActivityDoneButton"
        android:id="@+id/radioInputsActivityDoneButton"
        android:layout_centerHorizontal="true"
        android:layout_alignParentBottom="true"
        android:layout_marginBottom="@dimen/activity_horizontal_margin"/>
</RelativeLayout>

设计器中的图片

模拟器中的图片

此外,我的活动布局应要求提供:

Also, my activity layout as requested:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/main_content"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context="com.complexityresolved.lifetracker.RadioInputsActivity">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/appbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingTop="@dimen/appbar_padding_top"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:layout_scrollFlags="scroll|enterAlways"
            app:popupTheme="@style/AppTheme.PopupOverlay">

        </android.support.v7.widget.Toolbar>

    </android.support.design.widget.AppBarLayout>

    <android.support.v4.view.ViewPager
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />

</android.support.design.widget.CoordinatorLayout>

推荐答案

尝试一下:-

我已经对您进行了测试,我看到了您上面提到的问题.我已经更新了您的代码,并在下面发布了.运行良好.

I have tested your you, i saw the issue that you mentioned above. I have updated your code ande posted below. It is working good.

在您的Activity.xml中

In your Activity.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/main_content"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"      tools:context="com.complexityresolved.lifetracker.RadioInputsActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <android.support.design.widget.AppBarLayout
            android:id="@+id/appbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:paddingTop="@dimen/appbar_padding_top"
            android:theme="@style/AppTheme.AppBarOverlay">

            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:background="?attr/colorPrimary"
                app:layout_scrollFlags="scroll|enterAlways"
                app:popupTheme="@style/AppTheme.PopupOverlay">

            </android.support.v7.widget.Toolbar>

        </android.support.design.widget.AppBarLayout>

        <android.support.v4.view.ViewPager
            android:id="@+id/container"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_behavior="@string/appbar_scrolling_view_behavior" />

    </LinearLayout>
</android.support.design.widget.CoordinatorLayout>

这篇关于Android相对布局正在屏幕外运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆