我正试图在listadapter中扩展此资源:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="200dp"
android:layout_height="200dp"
android:background="#ff0000"
android:padding="@dimen/spacing_micro" >
<View android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="10dp"
android:background="#00ff00"/>
</FrameLayout>
使用以下代码:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewGroup view = (ViewGroup) convertView;
if (view == null) {
view = (ViewGroup) LayoutInflater.from(getContext()).inflate(R.layout.myview, parent, false);
}
return view;
}
尽管我用200dp的高度和宽度指定了
myview
的根,但它完全忽略了大小,导致大小为0x0。在代码中指定自定义高度和宽度会导致视图采用父视图的大小,再次忽略我的
200dp
首选项:ViewGroup.LayoutParams layoutParams = (ViewGroup.LayoutParams) view.getLayoutParams();
layoutParams.height = dpToPx(200);
layoutParams.width = dpToPx(200);
view.setLayoutParams(layoutParams);
我怎样才能达到我想要的,在200x200dp时有
View
的根,并且它的孩子的相对大小是match_parent
? 最佳答案
尝试使用指定了布局宽度和布局高度属性以匹配父布局的父布局包装您的框架布局。然后在父级中,可以设置帧布局的大小和重力-
关于android - 适配器中的膨胀布局忽略大小,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/16299896/