0

TextViews에 짧은 문장을 사용해야합니다. 나는 recyclerViewGridLayoutManager으로 사용하고 그리드의 각 셀에있는 TextView에 각 문장을 설정했습니다. 것은 격자의 각 셀이 TextView의 텍스트에 따라 너비를 변경하고 싶습니다. 즉, 긴 문장은 화면을 전체 너비로 가져갈 수 있습니다. 또는 문장이 매우 짧은 경우 동일한 줄에 있어야합니다.recyclerview에서 내부 텍스트 길이에 따라 열 너비가 변경되는 방법

또한 GridView을 사용해 보았지만 원하는 결과를 얻지 못했습니다.

어떻게 얻는 지 알고 싶습니까?

EDIT

item.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:custom="http://schemas.android.com/apk/res-auto" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:padding="5dp"> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:background="@color/unselected_term" 
     android:padding="10dp"> 

     <TextView 
      android:id="@+id/item_text" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:textColor="@color/term_text" 
      android:gravity="center" 
      android:textSize="18sp"/> 

</LinearLayout> 

EDIT 2

Desired layout

이것은 레이아웃이 어떻게 표시되는지 예입니다. 각 요소의 길이는 항상 같지 않습니다. 그래서 더 큰 너비가 더 짧은 너비를 갖는 레이아웃을 필요로합니다.

+0

여기에 xml을 게시 할 수 있습니까? 귀하가 recyclerview 항목 – Umair

+0

에 사용중인 레이아웃 @Umair Code added : –

+0

답변을 업데이트했습니다. 귀하를 위해 작동하는지 확인하고 알려주십시오. – Umair

답변

2

아래 레이아웃을 사용하면 텍스트에 따라 행 항목 크기가 변경됩니다. 이것을 시험해보고 효과가 있는지 확인하십시오.

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:custom="http://schemas.android.com/apk/res-auto" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:padding="5dp"> 

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:background="@color/colorPrimaryLight" 
    android:padding="10dp"> 

    <TextView 
     android:id="@+id/item_text" 
     android:layout_width="wrap_content" 
     android:layout_height="match_parent" 
     android:minWidth="100dp" 
     android:textColor="@color/primary_text" 
     android:gravity="center" 
     android:textSize="18sp"/> 

    </LinearLayout> 
</LinearLayout> 
+0

텍스트가 없을 때 너비를 유지하지 않으려면 android : textview의 minWidth 특성을 제거하십시오. – Umair

+0

도움을 주셔서 감사합니다. 그러나 요소가 항상 두 개의 열에 있기 때문에 문제를 해결하지는 못합니다. 내가 성취하고자하는 것을 더 잘 보여주기 위해 이미지를 업로드했습니다. –

+0

@ IvánRodríguezTorres까지 나는 당신이 더 큰 텍스트가 아래쪽 중간에 작은 텍스트로 표시되기를 원한다는 것을 이해합니다. 동시에 주어진 영역의 텍스트 크기에 따라 너비가 완전히 달라지기를 원하십니까? – Umair

2

RecycelrViewStaggeredGridLayoutManager을 사용해야합니다. 약 그것에 대해 StaggeredGridLayoutManager.

+0

좀 더 자세히 설명해 주시겠습니까? 나는 StaggeredGrid를 가지고 놀고 있었지만 원하는 결과를 얻지 못했습니다. 문제는 필자가 필요로하는 행과 열의 수가 동적이라는 것입니다. 그것은로드 된 텍스트에 따라 변경 될 것입니다. 나는 명확하게하기 위해 질문을 업데이트했다. –