获取Android中动态添加的LinearLayouts的高度
我在一个android应用程序中工作,我在Scollview中动态添加了一个线性布局。现在,我想动态添加每个线性布局的高度。下面是我的代码和Xml获取Android中动态添加的LinearLayouts的高度,android,android-layout,android-activity,android-linearlayout,Android,Android Layout,Android Activity,Android Linearlayout,我在一个android应用程序中工作,我在Scollview中动态添加了一个线性布局。现在,我想动态添加每个线性布局的高度。下面是我的代码和Xml LinearLayout layout = (LinearLayout) findViewById(R.id.subscrollLinearLayout); LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
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;
}
});
}
LinearLayout布局=(LinearLayout)findViewById(R.id.subscrollllinearlayout);
LayoutFlater充气器=(LayoutFlater)getSystemService(Context.LAYOUT\u充气器\u服务);
List mSempLog=new ArrayList();
mSempLog.add(“TEST1”);
mSempLog.add(“TEST2”);
mSempLog.add(“TEST3”);
mSempLog.add(“TEST4”);
mSempLog.add(“TEST5”);
mSempLog.add(“TEST6”);
mSempLog.add(“TEST7”);
mSempLog.add(“TEST8”);
对于(int i=0;i
Xml
行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>
这对我很有效
MyListViewObj.post(new Runnable(){
public void run(){
int height = MyListViewObj.getHeight();
}
});
事实上,你可以通过几种方法得到宽度
- 案例1:yourView.getHeight()(仅当视图已绘制时)
- 案例2:yourView.getLayoutParams().height(取决于LayoutParams)
- 案例3:
- 案例4:如果您有自定义布局,那么您可以在onMeasure(, int))
MyListViewObj.post(new Runnable(){
public void run(){
int height = MyListViewObj.getHeight();
}
});