【问题标题】:Empty layout when running app运行应用程序时布局为空
【发布时间】:2014-03-24 17:35:10
【问题描述】:

我正在尝试为我的 android 应用创建线性布局。在这个线性布局内部还有另外两种布局,一种是相对布局,另一种是线性布局。这两个布局的权重为 1。当我在 graphics_layout 查看布局时,一切看起来都很好。但是当我在手机或虚拟设备上运行我的应用时,我只会看到一个空布局。

这是应该的样子(实际上是graphic_layout的截图):

这是它在虚拟设备和我的手机上的外观:

这是我的 xml 代码:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#EEEEEE"
    android:orientation="vertical"
    android:paddingLeft="16dp"
    android:paddingRight="16dp"
    tools:context="xx.xx.counter.MainActivity">

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="2" >

        <ProgressBar
            android:id="@+id/progressBar2"
            style="?android:attr/progressBarStyleHorizontal"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:progressDrawable="@drawable/cpb"
            android:rotation="270" />

        <TextView
            android:id="@+id/textView1"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_centerInParent="true"
            android:text="@string/number"
            android:gravity="center"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:textSize="70sp" />
</RelativeLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1"
    android:orientation="vertical" >

    <Button
        android:id="@+id/button2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:text="@string/button2"
        android:visibility="gone" />

    <ProgressBar
        android:id="@+id/progressBar1"
        style="?android:attr/progressBarStyleHorizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:rotation="180" />
    <TextView 
        android:id="@+id/textView2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/number" />
</LinearLayout>
</LinearLayout>

这里是cpb.xml的代码:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@android:id/background">
    <shape
        android:innerRadiusRatio="3.5"
        android:shape="ring"
        android:thicknessRatio="60.0"
        android:useLevel="false">
        <gradient
            android:startColor="#AAAAAA"
            android:centerColor="#AAAAAA"
            android:endColor="#AAAAAA"
            android:type="linear"/>
    </shape>
</item> 
<item android:id="@android:id/progress">
    <shape
        android:innerRadiusRatio="4"
        android:shape="ring"
        android:thicknessRatio="20.0">
        <gradient 
            android:startColor="#ff8800"
            android:centerColor="#ff8800"
            android:endColor="#ff8800"
            android:type="linear"/>
    </shape>
</item>
</layer-list>

如果我将父线性布局的高度和宽度从 fill_parent 或 match_parent 更改为 wrap_content,它看起来并不像我想要的那样。但是我在运行应用程序时可以看到一些东西,所以我认为这不是 onCreate() – Methode 的问题。

那么我的布局有什么问题,我该怎么做才能让它看起来像我想要的那样?

【问题讨论】:

    标签: android android-layout layout android-xml


    【解决方案1】:

    我认为您的布局没有任何问题。它适用于我的 Nexus 5 手机。尝试改变

    android:layout_height="0dp"
    

    android:layout_height="wrap_content"
    

    另外,请确保您在 Activity OnCreate 上设置了 setContentView

    【讨论】:

    • 我在 Activity OnCreate 上设置了内容视图。当我将父线性布局更改为宽度和高度 wrap_content 时,我在运行应用程序时看到了一些东西。我也试过你的解决方案,运行应用程序时显示 0。仅此 0...
    【解决方案2】:

    我也没有发现任何问题。

    如果您使用权重,建议使用 0dp 宽度,但您可以尝试将 weightSum 属性添加到主 LinearLayout。

    android:weightSum="3"
    

    【讨论】:

      【解决方案3】:

      我找到了一个可行的解决方案。我不认为这是一个完美的方法,但它现在有效。

      我在其他布局周围放置了一个 FrameLayout,因此我的代码结构现在如下所示:

         <FrameLayout>
              <LinearLayout>
                  <RelativeLayout>
                  </RelativeLayout>
                  <LinearLayout>
                  </LinearLayout>
              </LinearLayout>
          </FrameLayout>
      

      有了这个解决方案,我得到了一个警告,LinearLayout,这实际上是我的主要布局,是不必要的。但我认为这是必要的,否则我无法使用 layout_weight。

      谁能告诉我,为什么它现在可以工作,但没有 FrameLayout 就不行?

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2014-08-18
        • 1970-01-01
        • 1970-01-01
        • 2021-06-16
        • 1970-01-01
        • 2016-01-05
        • 1970-01-01
        相关资源
        最近更新 更多