【问题标题】:android xml Layout issueandroid xml布局问题
【发布时间】:2011-01-25 16:26:15
【问题描述】:

我已经在这上面花了几个小时,这是一个布局问题。有人可以告诉我如何在同一个活动中同时显示图像和视图切换器。

<RelativeLayout 
            xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:myapp="http://schemas.android.com/apk/res/rg.client.muscle"
            android:layout_width="fill_parent"
        android:layout_height="fil_parent">
    <ViewSwitcher 
        android:id="@+id/relative"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" 
        android:layout_alignParentBottom="true">

        <!-- contains two relative layouts of same sizes 
        containing same elements just with switched positions -->

        <RelativeLayout 

            android:layout_width="fill_parent"
            android:layout_height="wrap_content" 
            android:layout_alignParentBottom="true" >

            <com.admob.android.ads.AdView 
                android:id="@+id/adv" 
                android:layout_width="fill_parent" 
                android:layout_height="wrap_content"
                android:layout_alignParentBottom="true"
                myapp:backgroundColor="#000000" 
                myapp:primaryTextColor="#FFFFFF" 
                myapp:secondaryTextColor="#CCCCCC"
                myapp:refreshInterval="30" >
            </com.admob.android.ads.AdView>

            <LinearLayout 
                android:id="@+id/linear" 
                android:orientation="horizontal" 
                android:weightSum="100" 
                android:layout_width="fill_parent" 
                android:layout_height="wrap_content" 
                android:layout_above="@id/adv" >

                <Button 
                    android:id="@+id/but_prev2" 
                    android:text="Previous" 
                    android:layout_width="0dp" 
                    android:layout_height="wrap_content" 
                    android:layout_weight="30" >
                </Button>

                <Button 
                    android:id="@+id/but_more2" 
                    android:text="More" 
                    android:layout_width="0dp" 
                    android:layout_height="wrap_content" 
                    android:layout_weight="40" >
                </Button>

                <Button 
                    android:id="@+id/but_next2" 
                    android:text="Next" 
                    android:layout_width="0dp" 
                    android:layout_height="wrap_content" 
                    android:layout_weight="30">
                </Button>
            </LinearLayout>
        </RelativeLayout>

        <RelativeLayout 

            android:layout_width="fill_parent"
            android:layout_height="wrap_content" 
            android:layout_alignParentBottom="true" >

            <LinearLayout 
                android:id="@+id/linear2" 
                android:orientation="horizontal"  
                android:weightSum="100" 
                android:layout_width="fill_parent"    
                android:layout_height="wrap_content" 
                android:layout_alignParentBottom="true" >

                <Button 
                    android:id="@+id/but_prev" 
                    android:text="Previous" 
                    android:layout_width="0dp" 
                    android:layout_height="wrap_content" 
                    android:layout_weight="30">
                </Button>

                <Button 
                    android:id="@+id/but_more" 
                    android:text="More" 
                    android:layout_width="0dp" 
                    android:layout_height="wrap_content" 
                    android:layout_weight="40">
                </Button>

                <Button 
                    android:id="@+id/but_next" 
                    android:text="Next" 
                    android:layout_width="0dp"   
                    android:layout_height="wrap_content" 
                    android:layout_weight="30">
                </Button>

            </LinearLayout>

            <com.admob.android.ads.AdView 
                android:id="@+id/adv2" 
                android:layout_width="fill_parent" 
                android:layout_height="wrap_content"
                android:layout_above="@id/linear2" 
                myapp:backgroundColor="#000000" 
                myapp:primaryTextColor="#FFFFFF" 
                myapp:secondaryTextColor="#CCCCCC"
                myapp:refreshInterval="30">
            </com.admob.android.ads.AdView>
        </RelativeLayout>
    </ViewSwitcher>

    <ImageView 
        android:id="@+id/image"
        android:layout_above="@id/relative"
        android:padding="2pt" 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:layout_alignParentTop="true" >
    </ImageView>
</RelativeLayout>

如果我将 imageview 放在首位,那么每次加载新图像时,按钮都会消失,整个布局似乎会重新膨胀

更新:

布局必须在顶部有一个图像视图,然后是一个视图切换器。

【问题讨论】:

    标签: android xml layout android-layout inflate


    【解决方案1】:

    我想这在很大程度上与您无法读取 XML 有关。如果它看起来像您最初在此处粘贴的方式,那就是。您的第一个问题是您的根 RelativeLayout 标记中没有指定大小。在大多数情况下,您的根布局应该设置为fill_parentlayout_width,与layout_height 相同。您还需要您的架构规范(即xmlns:android="http://schemas.android.com/apk/res/android",尽管我假设您只是忽略了它,因为您实际上正在编译它。

    您可能还需要记住,因为大多数东西的高度都设置为wrap_content,如果它超过屏幕的高度,其中一些将不可见,因此您可以考虑包裹内部变成ScrollView

    编辑:再次,我建议您将RelativeLayout(根元素)的高度设置为fill_parent。尝试先定义您的 ImageView,将其保留为 alignParentTop="true",然后定义您的 ViewSwitcher,并为其指定 layout_below="@id/image" 属性。

    【讨论】:

    • 对不起,我只是没有在此处添加您所说的内容。但它们都是在代码中设置的。
    • 如果您想要一个准确的答案,您可能应该添加它们。尝试将android:layout_below="@id/image" 添加到您的 ViewSwitcher。不知道你的布局和里面的内容,很难说。此外,RelativeLayouts 没有 android:orientation 属性,只有 LinearLayout。使用这些对 RelativeLayout 没有影响。
    • 修复了它并添加了图像必须存在的部分。
    • 您尝试过我的 layout_below 建议了吗?我已经更新了我的答案以反映这些变化。
    • 是的,我已经尝试过您的建议。它这样做。图像显示在整个屏幕上,一段时间后按钮出现在底部。每次我从互联网上获取图像时都会发生这种情况。
    【解决方案2】:

    还有小建议,使用include布局,会更清晰。

     <include layout="@layout/my_other_layout" />
    

    【讨论】:

    • 我想遵循这一点,现在我在处理事件和所有方面都遇到了麻烦。如果我使用 include 放入相同的布局,我怎样才能获得按钮的 id。
    猜你喜欢
    • 1970-01-01
    • 2013-01-10
    • 1970-01-01
    • 2021-12-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多