Android 将片段与RelativeLayout对齐
我在尝试将片段与Android 将片段与RelativeLayout对齐,android,android-layout,android-fragments,android-relativelayout,android-linearlayout,Android,Android Layout,Android Fragments,Android Relativelayout,Android Linearlayout,我在尝试将片段与RelativeLayout对齐时遇到了真正的问题,尽管这似乎应该很简单。我只需要两个相邻的片段,如果我使用LinearLayout它可以正常工作: <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="fill_parent"
RelativeLayout
对齐时遇到了真正的问题,尽管这似乎应该很简单。我只需要两个相邻的片段,如果我使用LinearLayout
它可以正常工作:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<fragment android:name="com.fragment.test.TitlesFragment"
android:id="@+id/fragmentTitles"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="fill_parent"
/>
<FrameLayout
android:id="@+id/details"
android:layout_weight="1"
android:layout_width="0px"
android:layout_height="fill_parent"
/>
</LinearLayout>
更新:
下面是我看到的屏幕截图:
这是我正在使用的代码:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<LinearLayout
android:orientation="horizontal"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:weightSum="3"
>
<fragment android:name="com.fragment1"
android:id="@+id/fragment1"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_above="@id/statusUpdated"
/>
<fragment android:name="com.fragment2"
android:id="@+id/fragment2"
android:layout_width="0px"
android:layout_weight="2"
android:layout_height="fill_parent"
android:layout_above="@id/statusUpdated"
android:layout_toRightOf="@id/fragment1"
/>
</LinearLayout>
<TextView android:id="@+id/statusUpdated" style="@style/Status" />
</RelativeLayout>
不能将权重与
相对长度一起使用。基本上,该属性被忽略,这意味着两个片段都以0
的宽度呈现,因此它们不可见
我不确定你想要完成什么,但是你可能想考虑把你的第一个例子(<代码>线性布局< /代码>)包装成<代码> ReleLayOut<<代码> -换句话说:合并/合并两个布局。但这确实会导致视图层次结构中的另一个级别
编辑:
我还没有实际测试过,但我想这就是你想要的:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:id="@+id/fragment_layout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_above="@+id/footer_view"
android:weightSum="3">
<fragment
android:id="@+id/fragmentTitles"
android:name="com.fragment.test.TitlesFragment"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1" />
<FrameLayout
android:id="@+id/details"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="2" />
</LinearLayout>
<TextView
android:id="@+id/footer_view"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="#f00"
android:text="I am a footer" />
</RelativeLayout>
显然,你可以用你喜欢的任何东西替换TextView
。我最终想做的是在布局的底部添加一个页脚,但我无法使用线性布局使其工作。我可以使用RelativeLayout
来显示页脚,但是片段不会出现,所以我只是想让片段出现(没有页脚),因此我的问题是。听起来我必须弄清楚如何使用LinearLayout
让页脚工作。我基本上想要一个布局,左边的片段占屏幕的1/3,右边占屏幕的其余部分,有一个粘性的页脚。对,这应该不太难实现。请看我答案中的编辑并试一试。我还没试过,但应该(接近)你想要的。我说得太快了。我实现了你的建议,大部分都有效,但页脚似乎与列表中的最后一项重叠,因此最后一项被切成两半。呃,我真的不擅长Android的布局。你能提供一个屏幕截图吗?如果我使用如上所示的布局,给页脚一个透明的背景,并为片段设置颜色,那么一切都会很好地保持在页脚上方。如果其中一个片段与页脚重叠,我应该看到页脚后面的颜色(因为透明度)。不客气-我们都有这样的夜晚。:)幸好我喜欢摆弄布局。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:id="@+id/fragment_layout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_above="@+id/footer_view"
android:weightSum="3">
<fragment
android:id="@+id/fragmentTitles"
android:name="com.fragment.test.TitlesFragment"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1" />
<FrameLayout
android:id="@+id/details"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="2" />
</LinearLayout>
<TextView
android:id="@+id/footer_view"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="#f00"
android:text="I am a footer" />
</RelativeLayout>