2017-10-29 1 views
0

내부에 여러 개의 자식 TextView가있는 LinearLayout (가로)가 있습니다. LinearLayout과 모든 TextViews에 고정 된 layout_width를 설정했습니다. 자녀의 전체 너비가 레이아웃의 너비를 초과하면 자동으로 자녀를 축소하여 좁게 만듭니다. 이는 내가 원하지 않는 것입니다. 레이아웃을 사용하여 어린이 콘텐트를 클립하고 폭을 유지하기를 원합니다. 어떻게 할 수 있습니까?Android 콘텐츠를 클립하기 위해 Android LinearLayout을 만드는 방법은 무엇인가요?

예 : 내 LinearLayout의 너비가 200dp이고 텍스트 너비가 60dp 인 텍스트 뷰가 4 개인 경우를 가정 해 보겠습니다. 처음 3 개의 TextViews와 4/3 (60 + 60 + 60 + 20 = 200)의 1/3을 표시하고 싶습니다.

감사합니다. LinearLayout

  • 그리고 android:layout_width="0dp"를 사용하고 android:layout_weight="1"에서

  • +0

    코드 붙여 넣기 –

    답변

    0
    • 사용 android:weightSum

    TextView이보십시오.

    <?xml version="1.0" encoding="utf-8"?> 
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
          android:layout_width="match_parent" 
          android:layout_height="match_parent" 
          android:orientation="horizontal" 
          android:weightSum="10"> 
    
    <TextView 
        android:layout_width="0dp" 
        android:layout_height="wrap_content" 
        android:layout_weight="3" 
        android:text="hello"/> 
    
    <TextView 
        android:layout_width="0dp" 
        android:layout_height="wrap_content" 
        android:layout_weight="3" 
        android:text="hello"/> 
    
    <TextView 
        android:layout_width="0dp" 
        android:layout_height="wrap_content" 
        android:layout_weight="3" 
        android:text="hello"/> 
    
    <TextView 
        android:layout_width="0dp" 
        android:layout_height="wrap_content" 
        android:layout_weight="1" 
        android:text="hello"/> 
    
    </LinearLayout> 
    

    또한

    당신은 TextViewandroid:layout_width="wrap_content"android:layout_width="match_parent"을 변경할 수 있습니다.

    편집

    그리고 당신은 당신의 자바 코드에서 사용할 수 있습니다.

    너비 나 체중을 직접 변경할 수 있습니다.

    private LinearLayout mLinearLayout; 
    private int viewsCount = 4; 
    
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.activity_main); 
    
        mLinearLayout = (LinearLayout) findViewById(R.id.linearlayout); 
        for (int i = 0; i < viewsCount; i++) { 
         TextView textView = new TextView(this); 
         if(i == viewsCount-1){ 
          LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(0, ViewGroup.LayoutParams.WRAP_CONTENT, 1); 
         } else { 
          LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(0, ViewGroup.LayoutParams.WRAP_CONTENT, 3); 
         } 
         mLinearLayout.addView(textView); 
        } 
    } 
    
    +0

    첫 번째 솔루션은 처음 세 개의 TextView를 표시하고 후속 제품은 숨 깁니다. 그리고 여전히 LinearLayout 공간을 처음 세 개 사이에 균등하게 나눕니다. 이는 제 경우가 아닙니다. LinearLayout이 TextView를 지정된 너비로 유지하여 최대한 많은 내용을 표시하고 나머지는 잘라내기를 원합니다. 예 : LinearLayout의 너비가 200dp이고 텍스트 너비가 60dp 인 텍스트 뷰가 4 개인 경우를 가정 해 보겠습니다. 처음 3 개의 TextViews와 4/3의 1/3을 보여주고 싶습니다. p.s. RelativeLayout 및 ConstraintLayout을 사용하여이 작업을 수행 할 수 있지만 LinearLayout을 사용하고 싶습니다. – ScottD

    +0

    '3,3,3,1'에 대한 가중치를 설정할 수 있습니다. – KeLiuyue

    +0

    마지막'textview' 평균을 원하지 않는다면,'TextView android : layout_width = "wrap_content" android : layout_height = "wrap_content" android : text = "hello"/>'를 사용할 수 있습니다. – KeLiuyue

    관련 문제