【问题标题】:What is the best way to create this Layout in Android?在 Android 中创建此布局的最佳方法是什么?
【发布时间】:2015-07-23 20:28:19
【问题描述】:

为了让这个布局发挥作用,我已经苦苦挣扎了好几个小时。

这是我的代码:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:orientation="horizontal"
    tools:context=".MainPreviewActivity">

    <fragment
        android:name="com.apps.foo.CleanPreviewFragment"
        android:id="@+id/clean_preview_fragment"
        android:layout_weight="0.5"
        android:layout_width="0dp"
        android:layout_height="match_parent">
    </fragment>

    <fragment
        android:name="com.apps.foo.pointop.DirtyPreviewFragment"
        android:id="@+id/filters_fragment"
        android:layout_weight="0.5"
        android:layout_width="0dp"
        android:layout_height="match_parent">
    </fragment>

    <fragment
        android:name="com.apps.foo.pointop.ProcessedPreviewFragment"
        android:id="@+id/processed_preview_fragment"
        android:layout_weight="1"
        android:layout_width="0dp"
        android:layout_height="match_parent">
    </fragment>

</LinearLayout>

每个片段都是一个简单的RelativeLayout(都具有相同的视图):

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.apps.<whatever the fragment name is>">
</RelativeLayout>

现在我想让它像这样工作:

  • 1) 没有嵌套的 layout_weight

  • 2) 根本没有嵌套(例如嵌套前 2 个片段等 等)

  • 3) 视图完成后不使用代码以编程方式执行此操作 渲染。

在我看来,最干净最易读的方法是将片段 1 和片段 2 的方向设置为水平,将片段 3 设置为垂直,但这不起作用。

我还检查了this answer, 但是这家伙,使用带有RelativeLayout 的layout_weight。相对布局会忽略权重,这将不起作用。

任何帮助将不胜感激?

【问题讨论】:

    标签: android android-layout android-fragments


    【解决方案1】:

    您可以使用RelativeLayout 在布局中心添加一个宽度和高度为0dp 的虚拟视图,并相应地定位您的片段:

    <RelativeLayout 
      ... >
    
      <View
        android:id="@+id/center_anchor"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_centerInParent="true" />
    
      <fragment
        android:layout_toLeftOf="@id/center_anchor"
        android:layout_above="@id/center_anchor"
        ... />
    
      <fragment
        android:toLeftOf="@id/center_anchor"
        android:layout_below="@id/center_anchor"
        ... />
    
      <fragment
        android:toRightOf="@id/center_anchor"
        ... />
    
    </RelativeLayout>
    

    【讨论】:

    • 是的,但就像我提到的那样,它在 relative_layout 中使用“android:layout_weight="1"”。这不违反规定吗?相对布局忽略权重对吗?那么为什么它甚至对那个人有用呢?
    • RelativeLayout 有一个LinearLayout 作为父级,所以layout_weight 可以工作。作用于权重的是父视图,LinearLayout 确实支持它。
    • 请看这里,stackoverflow.com/a/14009266/990194。还有一个问题,我的片段布局重要吗?
    • RelativeLayout 不关注android:layout_weight 属性其子项LinearLayout 确实如此,在这种情况下,RelativeLayoutLinearLayout 的孩子。
    • @TrtTrt,一些 attr 应用于视图,其他应用于他的父级。以防万一,放置 layout_weight 仅在其父级是 LinearLayout 时才起作用,否则将不起作用。 LinearLayout 将查看其子项是否具有权重属性来进行任何大小计算,如果它的视图位于 LinearLayout 内而不是正确使用它,则您可以安全地将权重属性应用于任何类型的视图。
    猜你喜欢
    • 1970-01-01
    • 2012-05-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-12
    • 1970-01-01
    • 2019-04-16
    • 2017-08-06
    相关资源
    最近更新 更多