gpt4 book ai didi

android - 如何更改父 View 的高度以适合 subview

转载 作者:行者123 更新时间:2023-11-29 00:25:56 25 4
gpt4 key购买 nike

我有一个包含一个 ListView 的相对 View 。我在运行时创建这个相对 View 并将内容(基本上是一个字符串数组)添加到我的 ListView 中。我已将 ListView 的宽度和高度设置为“wrap_content”。问题是我的列表父相对 View 没有调整大小以显示我的 ListView 。它只显示列表的第一个值。我知道我必须重写 onmeasure 方法。那么,我究竟如何才能在 onmeasure 方法中获得我的 ListView 的高度,以便我可以将它传递给我的父 View 我读过很多这样的问题,但无法解决。

还有一件事 getMeasuredHeight() 在我的 onmeasure() 方法中返回 0。

更新

我的主布局activity_main

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/layout_route_search"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<RelativeLayout
android:id="@+id/layout_main"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</RelativeLayout>

</ScrollView>

lo_block.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Text_view" />

<ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content" />

</RelativeLayout>

这是我的代码
fragment .java

public class Frag extends Fragment {

private View mView;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {

mView = inflater.inflate(R.layout.activity_main, null);

showView();
return mView;
}

public void showView() {

block card = new block(getActivity());

LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT);

card.setLayoutParams(params);
ViewGroup vg = (ViewGroup) mView.findViewById(R.id.layout_main);
vg.addView(card);
}

private class block extends RelativeLayout {

//int listViewh = 0;//private float listViewHeight;

    public block(Context context) {
super(context);

View mView = View.inflate(getContext(), R.layout.lo_block, this);

ListView lst = (ListView) mView.findViewById(R.id.listView1);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(
getActivity(), android.R.layout.simple_list_item_1,
new String[] { "ad", "sfsd", "sfsf", "asfsfs" });

lst.setAdapter(adapter);
}

public block(Context context, AttributeSet attrs, int defStyle) {

super(context, attrs, defStyle);

}

@Override
public int getMinimumHeight() {

return super.getMinimumHeight();
}

@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);

}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

super.onMeasure(widthMeasureSpec, heightMeasureSpec);

int w = MeasureSpec.getSize(widthMeasureSpec);
int h = MeasureSpec.getSize(heightMeasureSpec);

//setMeasuredDimension(w, h);

}

主.xml

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

</LinearLayout>

我的主要 Activity 公共(public)类 MainActivity 扩展 Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

getFragmentManager().beginTransaction().replace(R.id.fragment,
new Frag()).commit();
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}


}

最佳答案

在我的案例中,确切的问题是我试图将 listview 放在 scrollview 中。 并且通常不推荐。但我设法扩展了我的 ListView 。虽然它不能滚动,但比以前更好。下面是我在构造函数中添加的代码

ListView lst = (ListView) mView.findViewById(R.id.listView1);
String[] arrayList = new String[] { "ad", "sfsd", "sfsf", "asfsfs",
"ssfsff", "llkllk", "4323" };

ArrayAdapter<String> adapter = new ArrayAdapter<String>(
getActivity(), android.R.layout.simple_list_item_1,
arrayList);
int totalHeight = 0;
for (int i = 0; i < adapter.getCount(); i++) {
View listItem = adapter.getView(i, null, lst);
listItem.measure(0, 0);
totalHeight += listItem.getMeasuredHeight();
}
LayoutParams lp = (LayoutParams) lst.getLayoutParams();
int height = totalHeight;

// arraylist list is in which all data is kept

lp.height = height;
lst.setLayoutParams(lp);

lst.setAdapter(adapter);

关于android - 如何更改父 View 的高度以适合 subview ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19475045/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com