Android 运行应用程序时为空布局

Android 运行应用程序时为空布局,android,android-layout,layout,android-xml,Android,Android Layout,Layout,Android Xml,我正在尝试为我的android应用程序创建一个线性布局。在这个线性布局内部还有两个其他布局,一个是相对布局,另一个是线性布局。这两个布局的权重为1。当我在图形_布局中查看布局时,一切看起来都很好。但当我在手机或虚拟设备上运行我的应用程序时,我只看到一个空的布局 这就是它的外观(实际上是图形_布局的屏幕截图): 这是它在虚拟设备和我的手机上的外观: 以下是我的xml代码: <LinearLayout xmlns:android="http://schemas.android.com/ap

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

这就是它的外观(实际上是图形_布局的屏幕截图):

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

以下是我的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>

如果我将父线性布局的高度和宽度从“填充父级”或“匹配父级”更改为“换行内容”,则它的外观与我希望的不一样。但是,当我运行应用程序时,我可以看到一些东西,所以我不认为这是onCreate()–Methode的问题


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

我看不出你的布局有任何问题。它适用于我的Nexus5手机。尝试改变

android:layout_height="0dp"


另外,请确保在创建活动时设置了“内容视图”

我也看不到任何问题

如果您使用的是weigth,建议使用0dp宽度,但您可以尝试将weightSum属性添加到主线布局中

android:weightSum="3"

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

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

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

通过这个解决方案,我得到了一个警告,即线性布局(实际上是我的主布局)是不必要的。但我认为这是必要的,因为否则我不能使用布局和重量


有人能告诉我,为什么它现在可以工作,但没有框架布局就不行?

我在Activity OnCreate上设置了ContentView。当我将父线性布局更改为宽度和高度包裹内容时,我在运行应用程序时会看到一些东西。我还尝试了您的解决方案,在运行应用程序时会显示0。没什么,只是0。。。
   <FrameLayout>
        <LinearLayout>
            <RelativeLayout>
            </RelativeLayout>
            <LinearLayout>
            </LinearLayout>
        </LinearLayout>
    </FrameLayout>