【问题标题】:LinearLayout pushing TextView out of the screenLinearLayout 将 TextView 推出屏幕
【发布时间】:2016-03-25 20:33:12
【问题描述】:

我是 Android 应用开发的新手,但遇到了一些问题。

我有一个带有 2 个 ImageButtons 的 LinearLayout 和另一个带有 TextView 的 LinearLayout,如您在此处所见 -

Looks pretty-ish

但是,我尝试添加另一个 LinearLayout 以添加更多内容,但它开始将我的欢迎消息推出屏幕

I add another LinearLayout and this happens

这是屏幕的 XML

<?xml version="1.0" encoding="utf-8"?>
  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="fill_parent"
          android:layout_height="fill_parent"
          android:orientation="vertical"
          android:layout_weight="0.6"
          android:id="@+id/layout_home">

<ScrollView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:id="@+id/scrollView"
    >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:orientation="vertical"
        android:paddingLeft="0dp"
        android:paddingRight="0dp"
        >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:measureWithLargestChild="false"
            android:longClickable="false">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="fill_parent"
                android:textAppearance="?android:attr/textAppearanceLarge"
                android:text="Welcome"
                android:id="@+id/textView4"
                android:textSize="60dp"
                android:layout_weight="1"
                android:layout_gravity="top"/>
        </LinearLayout>

        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="56dp"
            android:paddingLeft="5dp"
            android:paddingRight="5dp"
            android:background="#5cfca1">

        </LinearLayout>

        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:paddingLeft="5dp"
            android:paddingRight="5dp">

            <ImageButton
                android:id="@+id/whatisit"
                android:layout_width="fill_parent"
                android:layout_height="100dp"
                android:layout_weight="0.5"/>

            <ImageButton
                android:id="@+id/contact"
                android:layout_width="fill_parent"
                android:layout_height="100dp"
                android:layout_weight="0.5"/>
        </LinearLayout>

        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:paddingRight="5dp"
            android:paddingLeft="5dp"
            android:paddingBottom="300dp">

            <ImageButton
                android:id="@+id/howcanitreatit"
                android:layout_width="fill_parent"
                android:layout_height="100dp"
                android:layout_weight="0.5"/>

            <ImageButton
                android:id="@+id/disclaimer"
                android:layout_width="fill_parent"
                android:layout_height="100dp"
                android:layout_weight="0.5"
                android:adjustViewBounds="false"/>
        </LinearLayout>

    </LinearLayout>
</ScrollView>

已编辑:Styles.xml

<resources>

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>


</style>

我查看了其他问题,但他们似乎在设置布局和拥有更多元素等方面做了很多不同的事情。

为什么要推送欢迎消息?我在这里做错了什么?

您也可以随意检查任何内容。我犯的错误越多越好

【问题讨论】:

  • 为什么要增加布局权重?
  • 发布style.xml 文件。
  • @ZeeshanShabbir ImageButtons 上的 layout_weight 用于拆分它们。其他任何事情都可能是实验中的错误
  • @dieter_h 已编辑,谢谢

标签: java android xml


【解决方案1】:

在这部分代码中(第二个LinearLayout):

<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:orientation="vertical"
        android:paddingLeft="0dp"
        android:paddingRight="0dp"
        >

取出:android:layout_gravity="center"

同时从最后一个 LinearLayout 中删除:android:paddingBottom="300dp"

这将使它显示出来,但是您经常使用硬编码的dp 值,这从来都不是一个好的做法,因为您必须满足许多不同的屏幕尺寸。

【讨论】:

  • 太棒了!它确实修复了它。非常感谢。我正在处理硬编码值,有人已经提到过,我正在编辑
  • 任何一个/两个建议都可以解决问题 - 你的硬编码 dp 值将内容推到屏幕边界之外 - 它根本不适合
  • 正式注明。再次感谢您:)
【解决方案2】:

试试这个

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:orientation="vertical"
    android:paddingLeft="0dp"
    android:paddingRight="0dp"
    >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:measureWithLargestChild="false"
        android:longClickable="false">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:text="Welcome"
            android:id="@+id/textView4"
            android:textSize="60dp"
            android:layout_weight="1"
            android:layout_gravity="top"/>
    </LinearLayout>

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="56dp"
        android:paddingLeft="5dp"
        android:paddingRight="5dp"
        android:background="#5cfca1">

    </LinearLayout>

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingLeft="5dp"
        android:paddingRight="5dp">

        <ImageButton
            android:id="@+id/whatisit"
            android:layout_width="fill_parent"
            android:layout_height="100dp"
            android:layout_weight="0.5"/>

        <ImageButton
            android:id="@+id/contact"
            android:layout_width="fill_parent"
            android:layout_height="100dp"
            android:layout_weight="0.5"/>
    </LinearLayout>

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingRight="5dp"
        android:paddingLeft="5dp"
        android:paddingBottom="300dp">

        <ImageButton
            android:id="@+id/howcanitreatit"
            android:layout_width="fill_parent"
            android:layout_height="100dp"
            android:layout_weight="0.5"/>

        <ImageButton
            android:id="@+id/disclaimer"
            android:layout_width="fill_parent"
            android:layout_height="100dp"
            android:layout_weight="0.5"
            android:adjustViewBounds="false"/>
    </LinearLayout>

</LinearLayout>

【讨论】:

  • 感谢您的帮助。不幸的是,这并没有什么不同(仍然有相同的结果)
【解决方案3】:

android_height 和 android_width 属性用于告诉屏幕如何渲染,欢迎消息有一个 fill_parent 属性,它基本上告诉它应该遍布整个屏幕,将其更改为包装内容,无论哪种方式现在的高度你给它的宽度可能比屏幕尺寸大,而且也是一个坏习惯,因为它与那里的 7300 多种不同的 android 手机不匹配,作为拇指规则,你应该将这些尺寸保留在 xml 中。 要了解更多信息,请阅读this 文章

【讨论】:

  • 这是大多数教程没有告诉你的。非常感谢您的建议!将查看此页面并稍后进行编辑
【解决方案4】:

尝试为 Welcome TextView 设置一个固定高度,如果这符合您的要求。

例如:android:layout_height="65dp"

【讨论】:

【解决方案5】:

尝试从第二个 LinearLayout 中删除以下 LOC。即:LinearLayout 就在ScrollView 之后。

android:layout_gravity="center"

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-11-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多