3

Я работаю в приложении Android, и я динамически добавляю Linear Layout в свой Scollview. Теперь я хочу динамически увеличить высоту каждой линейной компоновки. Ниже приведен мой код и Xml.Получите высоту динамически добавленного LinearLayouts в Android

LinearLayout layout = (LinearLayout) findViewById(R.id.subscrollLinearLayout); 
    LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

    List<String> mSempLog = new ArrayList<String>(); 

    mSempLog.add("TEST1"); 
    mSempLog.add("TEST2"); 
    mSempLog.add("TEST3"); 
    mSempLog.add("TEST4"); 
    mSempLog.add("TEST5"); 
    mSempLog.add("TEST6"); 
    mSempLog.add("TEST7"); 
    mSempLog.add("TEST8"); 

    for (int i = 0; i < mSempLog.size(); i++) { 

     LinearLayout service_row = (LinearLayout) inflater.inflate(
       R.layout.tesffff, null); 
     TextView dd = ((TextView) service_row.findViewById(R.id.test1)); 
     dd.setText(mSempLog.get(i)); 
     dd.setTag(i); 
     ((TextView) service_row.findViewById(R.id.test2)).setText(mSempLog 
       .get(i)); 

     layout.addView(service_row, i); 

    } 

    ScrollView parentScrollView = (ScrollView) findViewById(R.id.parentscroll); 
    ScrollView childScrollView = (ScrollView) findViewById(R.id.subscroll); 

    parentScrollView.setOnTouchListener(new View.OnTouchListener() { 

     @Override 
     public boolean onTouch(View v, MotionEvent event) { 

      findViewById(R.id.subscroll).getParent() 
        .requestDisallowInterceptTouchEvent(false); 
      return false; 
     } 
    }); 

    childScrollView.setOnTouchListener(new View.OnTouchListener() { 

     @Override 
     public boolean onTouch(View v, MotionEvent event) { 

      // Disallow the touch request for parent scroll on touch of 
      // child view 
      v.getParent().requestDisallowInterceptTouchEvent(true); 
      return false; 
     } 
    }); 

} 

Xml

<?xml version="1.0" encoding="utf-8"?> 
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/parentscroll" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:fillViewport="true" 
    android:orientation="vertical" > 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:orientation="vertical" > 

     <ScrollView 
      android:id="@+id/subscroll" 
      android:layout_width="match_parent" 
      android:layout_height="200dp" > 

      <LinearLayout 
       android:id="@+id/subscrollLinearLayout" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:orientation="vertical" > 
      </LinearLayout> 
     </ScrollView> 

     <TextView 
      android:id="@+id/test1" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:text="dfsdfsdf" /> 

     <TextView 
      android:id="@+id/test1" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:text="dfsdfsdf" /> 

     <TextView 
      android:id="@+id/test1" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:text="dfsdfsdf" /> 

     <TextView 
      android:id="@+id/test1" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:text="dfsdfsdf" /> 

     <TextView 
      android:id="@+id/test1" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:text="dfsdfsdf" /> 

     <TextView 
      android:id="@+id/test1" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:text="dfsdfsdf" /> 

     <TextView 
      android:id="@+id/test1" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:text="dfsdfsdf" /> 

     <TextView 
      android:id="@+id/test1" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:text="dfsdfsdf" /> 

     <TextView 
      android:id="@+id/test1" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:text="dfsdfsdf" /> 
    </LinearLayout> 

</ScrollView> 

Строка XML

<?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:padding="15dp" 
    android:weightSum="2" > 

    <TextView 
     android:id="@+id/test1" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:gravity="center" 
     android:clickable="true" 
     android:onClick="onTest1" 
     android:textColor="@android:color/black" /> 

    <TextView 
     android:id="@+id/test2" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:clickable="true" 
     android:gravity="center" 
     android:onClick="onTest2" 
     android:textColor="@android:color/black" /> 

</LinearLayout> 
+1

Это может быть полезно: [Как получить высоту LinearLayout] (Http: // STAC koverflow.com/questions/11964499/how-to-get-height-of-linearlayout) –

+0

Считаете ли вы, что используете ListView со своим адаптером? Btw, вы не должны иметь ScrollView внутри ScrollView –

+0

@HamidrezaSamadi Great. Спасибо за ответ. – Arun

ответ

1

Это работало для меня

MyListViewObj.post(new Runnable(){ 
    public void run(){ 
     int height = MyListViewObj.getHeight(); 
    } 
}); 
1

На самом деле вы можете получить ширину несколькими способами.

Смежные вопросы