加载的视图的布局参数被忽略。

39
我想编写自己的自定义视图(View),但是在LayoutParams方面遇到了问题。
我的想法是扩展ViewGroup(LinearLayout)。
public class MyView extends LinearLayout{
    public MyView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }
    public MyView(Context context) {
        super(context);
    }
    public void putContent(){
        setOrientation(HORIZONTAL);
        LayoutInflater inflater = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        for (int i = 0; i < 5; i++){
            View view = inflater.inflate(R.layout.item, null);
            TextView tv = (TextView)view.findViewById(R.id.item_text);
            tv.setText("Item " + i);
            addView(view);
        }
    }
}

正如你所看到的,putContent方法会膨胀一个项并将其添加到我的视图中。以下是一个项布局:

<?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:background="#FFFFFF">
    <TextView android:text="TextView"
    android:id="@+id/item_text"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textColor="#000000"/>
</LinearLayout>

主屏幕布局

<?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="vertical">
<TextView  
    android:layout_width="match_parent" 
    android:layout_height="wrap_content"
    android_layout_weight="1" 
    android:text="@string/hello"
    />
    <my.test.MyView
    android:id="@+id/my_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android_layout_weight="1"
    />
</LinearLayout>

活动代码

public class Start extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        MyView myView = (MyView)findViewById(R.id.my_view);
        myView.putContent();
    }
}
这是我得到的截图。 enter image description here 问题在于:项目根元素的属性被忽略了。
android:layout_width="match_parent"
android:layout_height="match_parent"

但是我希望得到类似于这样的结果(当使用这行代码代替addView(view);时,我得到了这个结果)

addView(view, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT, 1F));

enter image description here

如何在不使用硬编码的LayoutParams的情况下实现此结果呢?感谢任何帮助!

更新

我还查看了调试模式下的view变量字段-mLayoutParamsnull,当我使用addView(view, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT, 1F))将充气的view添加到父级时,它变成了非空。 enter image description here

但是刚加载的视图的子元素的mLayoutParams不为空。 为什么在视图充气时只忽略xml布局中根元素的LayoutParams?


没有你的实际代码,很难对你的代码进行评论... - Nanne
不是很清楚。我假设上面是您正在膨胀的XML吗?但是您代码中的“ll”是什么?或者您是否将某些内容添加到XML中,如果是,那么您要添加什么内容? - Nanne
在代码中动态创建了 ll,我将给定的 XML 填充并放入 ll 中。 - Max Komarychev
当我使用 new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT, 1F) 并将其传递给 addView 方法时,与 v 一起使用时,它能正常工作!(在这种情况下,边距被忽略) - Max Komarychev
3个回答

127

使用以下语句膨胀:

View view = inflater.inflate( R.layout.item /* resource id */,
                                         MyView.this /* parent */,
                                         false /*attachToRoot*/);

你的评论对我的大脑起了作用。attacktoroot曾经是我的噩梦。非常感谢。 - Warpzit
实际上,这些评论帮助识别出这个答案作为数十个类似答案中可行的解决方案。 - DonSteep
当父对象为空时该怎么办?我从XML中传入的参数在执行wm.addView时仍然被忽略。 - cegprakash

0

好的,如果我理解正确,您在ll中有上面提到的XML,并且您正在向其中添加一些内容(R.layout.item_layout)。

我看到的问题是:

  • 您的基本布局(给定的XML,加载到ll中)具有match_parent,但我们不知道它是否有父级。因此,行为很难猜测。
  • 您没有指定要添加的项目的布局参数(仍然假设R.layout.item_layout不是您显示的XML)。因此,可能会出现预期的行为,甚至是未定义的行为(因此预期是意外的)。

可能这里有太多的代码要发布,我不知道,但是您最好使用hierarchy viewer来检查您树中的视图具有哪些参数,以便您可以实际看到发生了什么。

还要注意并非所有视图都支持边距

尽管视图可以定义填充,但它不提供任何边距支持。然而,视图组提供了这样的支持。有关更多信息,请参阅ViewGroup和ViewGroup.MarginLayoutParams。

更新了帖子并更清晰地解释了问题,感谢关注! - Max Komarychev

0
在Android中,“layout_”参数是指当前视图在其父布局中的行为方式。在这种情况下,它的父级是ll,但我们看不到那个是什么。
至于LinearLayout内TextView的布局,由于LinearLayout是垂直的,所有子项的layout_height都会自动被“wrap_content”覆盖,以便它们可以正确地排列在一起。
现在问题是,你实际上看到了什么,你想看到什么?

我的项目在屏幕左侧对齐,但我希望它们填满所有可用空间。 - Max Komarychev
更新了帖子并更清晰地解释了问题,感谢关注! - Max Komarychev

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接