【问题标题】:Android fragment XML layout issueAndroid片段XML布局问题
【发布时间】:2013-01-10 23:43:06
【问题描述】:

我正在尝试实现我认为相当简单的片段布局,但我遇到了一些问题。任何人都可以粗略地为以下布局建议 XML 解决方案(我知道现在支持嵌套片段,但如果可能的话,我想避免这种情况):

"=============================="
| 111111111111111111 | 333333333 |
| 111111111111111111 | 333333333 |
| 111111111111111111 | 333333333 |
| 111111111111111111 | 333333333 |
| 111111111111111111 | 333333333 |
| 111111111111111111 | 333333333 |
| ===================| 333333333 |
| 222222222222222222 | 333333333 |
| 222222222222222222 | 333333333 |
"=============================="

【问题讨论】:

    标签: android xml layout android-fragments fragment


    【解决方案1】:

    这确实是一个简单的布局。

    这是你要找的吗?

    代码如下:

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:orientation="vertical" >
    
            <FrameLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:background="#ff0000" >
            </FrameLayout>
    
            <FrameLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="2"
                android:background="#00ff00" >
            </FrameLayout>
        </LinearLayout>
    
        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="2"
            android:background="#0000ff" >
        </FrameLayout>
    
    </LinearLayout>
    

    【讨论】:

    • 层次结构中的2级只是浪费资源和性能!
    • 感谢您的回复,但在阅读curious-creature.org/2009/02/22/android-layout-tricks-1 之后,我倾向于同意之前的评论。我想保持性能尽可能快。我以前从未使用过 TableLayout,但我要看看它是否能满足我的需要。
    • @user1003815 链接的文章建议使用 RelativeLayout 作为解决方案。就性能而言,这确实可能是更好的解决方案。但是请注意,您不能将android:layout_weight 与RelativeLayouts 一起使用。您必须改用固定的高度/宽度。我不确定这对您的片段布局是否可行?另外:当您使用这样的布局时,RelativeLayout 的性能优势将更加重要,例如对于 ListView 中的每个项目。对于像您这样的简单片段布局,这不会有很大的不同。
    • 是的,RelativeView 中缺乏对 layout_weight 的支持是我转向 TableLayout 的原因,但最后我只是采用了这个答案中提供的解决方案(FrameLayout 被片段替换)。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-13
    • 2023-03-14
    • 1970-01-01
    • 1970-01-01
    • 2021-09-26
    相关资源
    最近更新 更多