简体   繁体   English

自定义视图中儿童的宽度

[英]Width of child in custom view

I'm woking on my first custom view which extends LinearLayout. 我正在唤醒扩展LinearLayout的第一个自定义视图。 The view consists two parts header view and content view. 该视图包括标题视图和内容视图两部分。 Header view is on top and has set margin from right. 页眉视图在顶部,并且从右侧设置了边距。 Content view is below: 内容视图如下: 在此处输入图片说明

I achive header margin with : view.layout(...) in overridden method protected void onLayout(...) in xml preview everything looks great but when I set TextView as header view with set text and gravity center I found out that text is ignoring borders it looks like (black is visible, gray invisible): 我在覆盖的方法中使用: view.layout(...)实现了标题页边距,在xml预览中protected void onLayout(...)看起来一切都很好,但是当我将TextView设置为带有设置文本和重心的标题视图时,我发现了该文本正在忽略看起来像的边框(黑色可见,灰色不可见): 在此处输入图片说明

But I need to accept borders: 但是我需要接受边界: 在此处输入图片说明

I tried to set layoutParams to headerView in protected void onLayout(...) method which solved my problem but I got infinity warning: requestLayout() improperly called by android.widget.TextView... another thing I try is to set layoutParams in protected void onMeasure method but in xml preview text has vertical orientation and looks like: 我试图在protected void onLayout(...)方法protected void onLayout(...)设置为headerView,这解决了我的问题,但我得到了无限警告: requestLayout() improperly called by android.widget.TextView...我尝试做的另一件事是在中设置layoutParams protected void onMeasure方法,但在xml预览中,文本具有垂直方向,如下所示:

t
h
i
s
...

so I think that this two approaches are not correct, therefore my question is how to set header view width in correct way? 所以我认为这两种方法都不正确,因此我的问题是如何以正确的方式设置标题视图宽度? thanks 谢谢

You create your view in java code? 您使用Java代码创建视图? Use XML to create your view otherwise post full code 使用XML创建视图,否则发布完整代码

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM