LayoutInflater가 지정한 layout_width 및 layout_height 레이아웃 매개 변수를 무시하는 이유는 무엇입니까?
LayoutInflater가 예상대로 작동하는 데 심각한 문제가 있었고 다른 사람들도 마찬가지였습니다. 런타임에 레이아웃을 사용하여 레이아웃을 추가하는 방법 .
LayoutInflater가 지정한 레이아웃 매개 변수를 무시하는 이유는 무엇입니까? 예를 들어 내 리소스 XML 의 layout_width
및 layout_height
값이 존중되지 않는 이유는 무엇입니까?
LayoutInflater 문서를 참조 하고 작은 샘플 데모 프로젝트를 설정 하여이 문제를 조사했습니다 . 다음 자습서는를 사용하여 레이아웃을 동적으로 채우는 방법을 보여줍니다 LayoutInflater
.
시작하기 전에 어떤 LayoutInflater.inflate()
매개 변수가 다음과 같은지 확인하십시오 .
- 자원 : 부하에 대한 XML 레이아웃 리소스 ID (예
R.layout.main_page
) - 루트 : 옵션보기 (경우에 생성 된 계층 구조의 부모가 될
attachToRoot
것입니다true
다른 사람), 또는 단순히 세트 제공하는 객체LayoutParams
반환 된 계층 구조의 루트 값을 (경우attachToRoot
입니다false
.) attachToRoot : 팽창 된 계층 구조를 루트 매개 변수에 첨부 해야하는지 여부 false 인 경우 root는
LayoutParams
XML에서 루트 뷰에 대한 올바른 서브 클래스를 작성하는 데만 사용됩니다 .반환 : 팽창 된 계층의 루트 뷰. 루트가 공급 된 경우
attachToRoot
이며true
,이 루트입니다; 그렇지 않으면 팽창 된 XML 파일의 루트입니다.
이제 샘플 레이아웃과 코드를 살펴 보겠습니다.
메인 레이아웃 ( main.xml
) :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent">
</LinearLayout>
이 컨테이너에 추가 된 TextView는 XML ( red.xml
) 에서 레이아웃 매개 변수가 성공적으로 적용된 경우 작은 빨간색 사각형으로 표시됩니다 .
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="25dp"
android:layout_height="25dp"
android:background="#ff0000"
android:text="red" />
이제는 LayoutInflater
다양한 통화 매개 변수와 함께 사용됩니다
public class InflaterTest extends Activity {
private View view;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ViewGroup parent = (ViewGroup) findViewById(R.id.container);
// result: layout_height=wrap_content layout_width=match_parent
view = LayoutInflater.from(this).inflate(R.layout.red, null);
parent.addView(view);
// result: layout_height=100 layout_width=100
view = LayoutInflater.from(this).inflate(R.layout.red, null);
parent.addView(view, 100, 100);
// result: layout_height=25dp layout_width=25dp
// view=textView due to attachRoot=false
view = LayoutInflater.from(this).inflate(R.layout.red, parent, false);
parent.addView(view);
// result: layout_height=25dp layout_width=25dp
// parent.addView not necessary as this is already done by attachRoot=true
// view=root due to parent supplied as hierarchy root and attachRoot=true
view = LayoutInflater.from(this).inflate(R.layout.red, parent, true);
}
}
매개 변수 변형의 실제 결과는 코드에 문서화되어 있습니다.
시놉시스 :LayoutInflater
루트를 지정하지 않고 호출 하면 XML의 레이아웃 매개 변수를 무시하고 호출이 팽창합니다. 루트 동일하지와 부풀려를 호출 null
하고하는 것은 attachRoot=true
레이아웃 매개 변수를로드 않지만, 반환 (당신이 그것을 사용 발견 할 수있는 경우를 제외하고는로드 된 객체에 추가 레이아웃 변경을 방지 다시 루트 객체 findViewById()
). 따라서 가장 많이 사용하려는 호출 규칙은 다음과 같습니다.
loadedView = LayoutInflater.from(context)
.inflate(R.layout.layout_to_load, parent, false);
To help with layout issues, the Layout Inspector is highly recommended.
andig is correct that a common reason for LayoutInflater ignoring your layout_params would be because a root was not specified. Many people think you can pass in null for root. This is acceptable for a few scenarios such as a dialog, where you don't have access to root at the time of creation. A good rule to follow, however, is that if you have root, give it to LayoutInflater.
I wrote an in-depth blog post about this that you can check out here:
https://www.bignerdranch.com/blog/understanding-androids-layoutinflater-inflate/
wanna add to main answer above
I tried to follow it but my recyclerView began to stretch every item to a screen
I had to add next line after inflating for reach to goal
itemLayoutView.setLayoutParams(new RecyclerView.LayoutParams(RecyclerView.LayoutParams.MATCH_PARENT, RecyclerView.LayoutParams.WRAP_CONTENT));
I already added these params by xml but it didnot work correctly
and with this line all is ok
'IT' 카테고리의 다른 글
Visual Studio Code : 줄 끝을 표시하는 방법 (0) | 2020.05.31 |
---|---|
CAP 정리-가용성 및 파티션 공차 (0) | 2020.05.31 |
Objective-C가 NSString을 전환 할 수 있습니까? (0) | 2020.05.31 |
ASP.NET CORE에서 클라이언트 IP 주소를 어떻게 얻습니까? (0) | 2020.05.31 |
이전 기능이 완료된 후 기능 호출 (0) | 2020.05.31 |