2017-10-29 3 views
0

私はLinearLayout(水平)内にいくつかの子TextViewsを持っています。私はLinearLayoutとTextViewsの両方に対してfixed layout_widthを設定しました。子の幅の合計がレイアウトの幅を超えると、自動的に子を縮小し、幅を狭くします。これは私が望むものではありません。私はレイアウトが子供のコンテンツをクリップし、幅を保持したい。 どうすれば実現できますか?Android LinearLayoutで子供のコンテンツをクリップする方法は?

例:LinearLayoutの幅が200dpで、幅が60dpのTextViewが4つあるとします。最初の3つのTextViewと、4番目の(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"を変更することができます。

    編集

    そして、あなたはあなたのJavaコードで使用することができます。

    あなた自身で幅や体重を変更することができます。

    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

    1番目のソリューションは、最初の3つのTextViewを表示し、後続のものを非表示にします。そして、それはまだ私の場合ではない最初の3つの間でLinearLayout空間を均等に分割します。私は、LinearLayoutがTextViewを指定された幅に保ち、できるだけ多くのコンテンツを表示し、残りの部分を切り取るようにします。例えば。 LinearLayoutの幅が200dpで、幅が60dpの4つのTextViewがあるとします。最初の3つのTextViewと、4番目の1/3のTextViewを表示します。 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

    関連する問題