RelativeLayout에서 병합 및 포함 작업을 수행하는 방법은 무엇입니까?
며칠 동안 여러 수준의 중첩 LinearLayouts
을 사용하여 변환하여 레이아웃을보다 효율적으로 만들려고 노력했지만 RelativeLayout
해결 방법을 찾지 못한 몇 가지 문제가 발생했습니다 ...
Android 초보자 그룹과이 사이트를 검색했지만 문제를 해결하는 데 도움이되는 항목을 찾지 못했습니다.
레이아웃 중 병합 및 포함 태그를 결합 할 수있는 블로그 중 하나를 읽었습니다. 그래서 내가 가진 것은 RelativeLayout
루트 요소 가있는 기본 레이아웃 파일입니다 . 그 안에는 루트에 대한 병합 요소가있는 5 개의 서로 다른 xml 레이아웃 파일을 참조하는 5 개의 포함 태그가 있습니다 (내 병합 파일은 모두 ID를 제외하고 동일합니다).
레이아웃 코드의 단순화 된 버전을 게시 한 후 설명 할 두 가지 문제가 있습니다.
샘플 메인 레이아웃 파일 :
<?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"
android:background="@drawable/translucent_gray" >
<include
android:id="@+id/running_gallery_layout_id"
layout="@layout/running_gallery_layout" />
<include
android:id="@+id/recent_gallery_layout_id"
layout="@layout/recent_gallery_layout"
android:layout_below="@id/running_gallery_layout_id" />
<include
android:id="@+id/service_gallery_layout_id"
layout="@layout/service_gallery_layout"
android:layout_below="@id/recent_gallery_layout_id" />
<include
android:id="@+id/process_gallery_layout_id"
layout="@layout/process_gallery_layout"
android:layout_below="@id/service_gallery_layout_id" />
</RelativeLayout>
샘플 포함 병합 파일 :
<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android">
<TextView
style="@style/TitleText"
android:id="@+id/service_gallery_title_text_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="left"
android:text="@string/service_title" />
<Gallery
android:id="@+id/service_gallery_id"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_below="@id/service_gallery_title_text_id" />
<TextView
style="@style/SubTitleText"
android:id="@+id/service_gallery_current_text_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/service_gallery_title_text_id"
android:layout_above="@id/service_gallery_id" />
</merge>
두 가지 문제가 있습니다.
1) android:layout_*
include 태그에 사용될 때 속성이 무시되는 것처럼 보이며 병합 된 모든 레이아웃이 서로 위에 표시됩니다. 이 게시물에 따르면 ( http://developer.android.com/resources/articles/layout-tricks-reuse.html ) "모든 android:layout_*
속성을 <include />
태그 와 함께 사용할 수 있습니다 "
2) Since I couldn't get this working I decided to try adding an android:layout_below
attribute to the first TextView
item in each merge layout file, meaning that each merge file would be referencing an id from another merge layout file... For the most part this actually worked and my layout looks fine. However, I get an error on one of the android:layout_below
attributes saying that it can't find the id I specified... I have double and triple checked the ids to make sure they were correct. The weirdest part is that I used the AutoFill
feature to put the id in the attribute in the first place.
If anyone has any suggestions or workarounds I will be more than happy to try them out. Also, if anyone can think of a way for me to just have one merge xml layout file instead of 5 that would be greatly appreciated. I couldn't find a way to do that because I need to have access to each item in the merge layout files at runtime...
There is an issue with the include tag. Check: https://issuetracker.google.com/issues/36908001
To fix it, make sure you overwrite BOTH layout_width
and layout_height
when including, otherwise everything will be ignored.
See the more highly voted answer below. Mine is woefully outdated
i can address one issue Justin raised: inability of RelativeLayout to manage positioning of an include (at least in this simple case, on a 1.6 emulator)
CommonsWare suggests wrapping the includes in a unique parent container, but does so in order to assist addressing & scoping identically named Views within Justin's includes
Each would have to have a unique parent container, and you would call findViewById() on that container (ViewGroup) rather than on the Activity.
In fact, you also must do it in order to get RelativeLayout to behave as expected:
This works (footer is well positioned):
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent">
<include android:id="@+id/header" layout="@layout/header"
android:layout_alignParentTop="true" />
<WebView android:id="@+id/webView" android:layout_below="@id/header"
android:background="#77CC0000" android:layout_height="wrap_content"
android:layout_width="fill_parent" android:focusable="false" />
<LinearLayout android:layout_alignParentBottom="true"
android:layout_height="wrap_content" android:layout_width="fill_parent">
<include android:id="@+id/footer" layout="@layout/footer" />
</LinearLayout>
</RelativeLayout>
This does not (footer is floating at top of screen):
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent">
<include android:id="@+id/header" layout="@layout/header"
android:layout_alignParentTop="true" />
<WebView android:id="@+id/webView" android:layout_below="@id/header"
android:background="#77CC0000" android:layout_height="wrap_content"
android:layout_width="fill_parent" android:focusable="false" />
<include android:id="@+id/footer" layout="@layout/footer"
android:layout_alignParentBottom="true" />
</RelativeLayout>
The bare footer include will not align to bottom of parent, without the surrounding LinearLayout.. I wouldn't call this expected behavior.
Additionally, the WebView appears to attach itself nicely to the header by ID, but I believe this to be illusion, due to it simply flowing below the header vertically. I also tried to set a button right above the footer include, but it got all floaty and wrong, too
RelativeLayout had more problems in 1.5, but i still like it :)
Man, this is old, but it seems to come up at the top of searches, so I'm going to comment.
I think the trick here is that the <merge>
tag combined with the <include>
tag essentially remove any sort of "parent" view group at that level. So then, who exactly are you asking to "layout_below" someone else? No one. There is no view at that level.
The <merge>
tag takes the child views and pops them right into the parent of the <include>
tag. You must therefore ask the children in the layout you're including to anchor themselves accordingly.
For positioning to work on RelativeLayout you need to set the layout_* parameters in the include file, not in the main layout file. That way
main_layout.xml
<RelativeLayout
android:id="@+id/header"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
....
</RelativeLayout>
<RelativeLayout
android:id="@+id/footer"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true">
.....
</RelativeLayout>
<include layout="@layout/content_layout" />
content_layout.xml
<merge xmlns:android="http://schemas.android.com/apk/res/android">
<RelativeLayout
android:id="@+id/content"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_above="@id/footer"
android:layout_below="@id/header" >
....
</RelativeLayout>
</merge>
This is obviously not what us developers want, but it's the only solution I've found to avoid duplicating xml
The android:layout_* attributes seem to be ignored when used in the include tag and all of the merged layouts are displayed on top of each other.
My guess is that you cannot reference, from layout rules, android:id
attributes that are defined on <include>
elements, only ones that are on "real" widgets and containers.
Also, if anyone can think of a way for me to just have one merge xml layout file instead of 5 that would be greatly appreciated.
Simple: put them all in one file.
I couldn't find a way to do that because I need to have access to each item in the merge layout files at runtime
Whether you have one <include>
element or 1,000, all of the contents should be accessible at runtime. One exception is if you have duplicated android:id
attributes -- you would need to properly scope your findViewById()
calls to get the right one, just like you have to when getting widgets from a ListView row.
If you can create a sample project that uses 2+ merge files where you can demonstrate that the contents are not accessible at runtime, let me know.
In my case, the layout which I was trying to include starts with <merge
tag. When I changed it to a layout, say <RelativeLayout
it worked. Below is the illustration.
WORKING
<RelativeLayout xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android"
tools:showIn="@layout/activity_home">
NOT WORKING
<merge xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android"
tools:showIn="@layout/activity_home">
I had the same problem and even defining layout_width
and layout_height
it didn't work. The problem was that the layout I was including had tags and after removing them, everything worked like a charm. I suppose that merge is not a layout tag and because of that, it cannot receive positioning and size parameters. Since everything you define in is transferred to the inner parent layout, the settings just were throw away.
TL:DR: Just remove the tags, move the xmlns definitions to a real layout viewholder and you should be good.
Before:
<merge
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<...ui.component.BorderCardView
android:layout_width="112dp"
android:layout_height="32dp"
app:cardCornerRadius="4dp"
app:cardUseCompatPadding="true">
<ImageView
android:layout_width="16dp"
android:layout_height="16dp"
android:src="@drawable/ic_logout"
android:tint="@color/divider" />
</...ui.component.BorderCardView>
</merge>
Working:
<...ui.component.BorderCardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="112dp"
android:layout_height="32dp"
app:cardCornerRadius="4dp"
app:cardUseCompatPadding="true">
<ImageView
android:layout_width="16dp"
android:layout_height="16dp"
android:src="@drawable/ic_logout"
android:tint="@color/divider" />
</...ui.component.BorderCardView>
참고URL : https://stackoverflow.com/questions/2316465/how-to-get-relativelayout-working-with-merge-and-include
'IT story' 카테고리의 다른 글
실행 일시 중지, 잠자기, R에서 X 초 동안 기다리는 방법은 무엇입니까? (0) | 2020.07.26 |
---|---|
pip freeze 명령 출력에서“pkg-resources == 0.0.0”이란 무엇입니까 (0) | 2020.07.26 |
하나의 명령으로 외래 키 제약 조건을 가진 새 열 추가 (0) | 2020.07.25 |
Git 저장소 컨텐츠를 다른 저장소 보존 히스토리로 이동 (0) | 2020.07.25 |
C #에서 사용하지 않는 지시문을 제거하는 이유는 무엇입니까? (0) | 2020.07.25 |