【问题标题】:Having an issue with layout cutoff/offset in my view在我看来,布局截止/偏移有问题
【发布时间】:2010-12-13 10:15:47
【问题描述】:

我的布局以纵向/横向模式显示时遇到问题。基本上,我的布局的层次结构是这样的:Main(工作区视图)-> TextView + Listview(w/ BaseAdapter)-> Listview Item。

我的问题是,如果我从纵向模式开始,我的项目会正确显示。但是如果我旋转设备,我的整个布局会移动大约半个屏幕(即当我切换时,视图左侧大约一半是完全黑色的,它认为我真正的“左边缘”是在屏幕中间 - 我可以看到应该在中间左对齐的项目)。另一方面,如果我从横向模式开始并切换到纵向模式,屏幕右侧的一半是完全黑色的,“右边缘”在中间,我可以看到我最右边的项目在那里结束。

这是我正在使用的布局... 首先是我在工作区中的视图(Listview 使用 BaseAdapter):

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

    <TextView android:id="@+id/tvMainChannel"
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="Disconnected"
        android:textColor="#FFFFFF"
        android:singleLine="true"/>

    <ListView android:id="@android:id/list"
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content"/>
</LinearLayout>

这是我个人的列表视图项目,它们被膨胀到上面的列表视图中(水平排列,每个项目应该从左到右显示:图像、文本、图像):

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="wrap_content"
    android:layout_width="fill_parent"
    android:orientation="horizontal">

    <ImageView android:id="@+id/images"
        android:layout_height="30dp"
        android:layout_width="wrap_content"
        android:paddingLeft="5px"
        android:paddingRight="5px"
        android:layout_alignParentLeft="true"/>

    <ImageView android:id="@+id/images_ping"
        android:layout_height="30dp"
        android:layout_width="wrap_content"
        android:paddingLeft="5px"
        android:paddingRight="5px"
        android:layout_alignParentRight="true"/>

    <TextView android:id="@+id/text"
        android:layout_height="30dp"
        android:layout_width="wrap_content"
        android:gravity="center_vertical"
        android:text="Username"
        android:textColor="#FFFFFF"
        android:layout_toRightOf="@+id/images"/>

</RelativeLayout>

我尝试在线性和相对之间切换,但我没有找到任何地方......对于这可能是什么原因感到不知所措。

非常感谢任何见解。

谢谢!

编辑:为了更清楚一点,这是我使用 Workspace 的 main.xml 文件:

<?xml version="1.0" encoding="utf-8"?>
<com.test.MultiView xmlns:app="http://schemas.android.com/apk/res/com.test"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/mvMain"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    app:default_screen="0">

    <include android:id="@+id/main_rtb" layout="@layout/main_rtb" />
    <include android:id="@+id/main_listview" layout="@layout/main_listview" />
</com.test.MultiView>

【问题讨论】:

    标签: android android-layout


    【解决方案1】:

    更新

    这个问题的实际答案是自定义顶级布局组件的问题,最初从问题中省略,并且改编自 stackoverflow 上其他地方的线程。有关详细信息,请参阅此答案的 cmets。

    原始答案(完全不正确,但并非完全没用)

    问题必须在您的项目布局中。建议使用这样的 LinearLayout('orientation' 属性对 RelativeLayout 无效):

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_height="wrap_content"
        android:layout_width="fill_parent"
        android:orientation="horizontal">
    
        <ImageView android:id="@+id/images"
            android:layout_height="30dp"
            android:layout_width="wrap_content"
            android:paddingLeft="5px"
            android:paddingRight="5px"
            />
    
    
        <TextView android:id="@+id/text"
            android:layout_height="30dp"
            android:layout_width="wrap_content"
            android:layout_weight="1"
            android:gravity="center_vertical"
            android:text="Username"
            android:textColor="#FFFFFF"
            />
    
        <ImageView android:id="@+id/images_ping"
            android:layout_height="30dp"
            android:layout_width="wrap_content"
            android:paddingLeft="5px"
            android:paddingRight="5px"
            />
    
    </LinearLayout>
    

    注意android:layout_weight 的使用以及RelativeLayout 特定属性android:layout_alignParentLeftandroid:layout_alignParentRight 的删除。

    【讨论】:

    • 感谢您的回复。这似乎不起作用。 TextView (@+id/tvMainChannel) 和 ListView (@android:id/list) 仍然在 x 轴上偏移,并且从屏幕右边缘开始的纵向视图的确切宽度开始向左走(即开始 x = 横向右边缘 - 纵向宽度)。
    • 哦,我明白了。您的清单文件中的 android:screenOrientation 设置是什么?
    • 我的清单文件中没有一套;但是,我正在使用 android:configChanges="keyboardHidden|orientation" 来覆盖我的活动,以免在屏幕旋转时被破坏
    • 您是否安装了一些奇怪/损坏的软键盘?如果通过长按更改 textview 上的输入法会发生什么?很抱歉猜测了这么多...我怀疑您的问题根本不在您发布的代码中。
    • 另外,这是什么设备,什么版本的Android?
    猜你喜欢
    • 2014-12-12
    • 2021-11-10
    • 1970-01-01
    • 2020-05-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多