将 XML 布局扩展为自定义 ViewGroup
我正在尝试将 xml 布局膨胀到自定义 ViewGroup
中。膨胀布局后,ViewGroup
仅显示布局的根视图,实际上它应该将完整的布局文件显示到 ViewGroup
中。
我已经浏览了 stackoverflow 和其他网站上发布的与此相关的其他类似问题,但没有任何帮助。
这是我的自定义 ViewGroup
的代码:
public class ViewEvent extends ViewGroup {
private final String LOG_TAG="Month View Event";
public ViewEvent(Context context) {
super(context);
}
public ViewEvent(Context context,AttributeSet attrs) {
super(context,attrs);
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
int childCount= getChildCount();
for(int i=0;i<childCount;i++)
{
Log.i(LOG_TAG, "Child: " + i + ", Layout [l,r,t,b]: " + l + "," + r + "," + t + "," + b);
View v=getChildAt(i);
if(v instanceof LinearLayout)
{
Log.i(LOG_TAG, "Event child count: " + ((ViewGroup)v).getChildCount()); // displaying the child count 1
v.layout(0, 0, r-l, b-t);
}
//v.layout(l, r, t, b);
}
}
在 Activity
中,我使用此自定义视图组:
ViewEvent event = new ViewEvent(getApplicationContext());
LayoutInflater inflator;
inflator=(LayoutInflater)appContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflator.inflate(R.layout.try1, event);
setContentView(event);
以下是布局文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ffffaa77">
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ffff0000"
android:text="asdasadfas" />
</LinearLayout>
膨胀此布局后,我只得到根 LinearLayout
背景颜色为 #ffff0000
。 TextView
未显示。
我哪里做错了或者遗漏了什么?
I am trying to inflate xml layout into custom ViewGroup
. After inflating the layout, ViewGroup
only displays the root view of the layout in-fact it should display the full layout file into the ViewGroup
.
I have gone through the other similar questions posted related to this on stackoverflow and other websites but none helped.
Here is the code of my custom ViewGroup
:
public class ViewEvent extends ViewGroup {
private final String LOG_TAG="Month View Event";
public ViewEvent(Context context) {
super(context);
}
public ViewEvent(Context context,AttributeSet attrs) {
super(context,attrs);
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
int childCount= getChildCount();
for(int i=0;i<childCount;i++)
{
Log.i(LOG_TAG, "Child: " + i + ", Layout [l,r,t,b]: " + l + "," + r + "," + t + "," + b);
View v=getChildAt(i);
if(v instanceof LinearLayout)
{
Log.i(LOG_TAG, "Event child count: " + ((ViewGroup)v).getChildCount()); // displaying the child count 1
v.layout(0, 0, r-l, b-t);
}
//v.layout(l, r, t, b);
}
}
In an Activity
, I am using this custom view group:
ViewEvent event = new ViewEvent(getApplicationContext());
LayoutInflater inflator;
inflator=(LayoutInflater)appContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflator.inflate(R.layout.try1, event);
setContentView(event);
Following is the layout file:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ffffaa77">
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ffff0000"
android:text="asdasadfas" />
</LinearLayout>
After inflating this layout, I am only getting the root LinearLayout
with backgroud color #ffff0000
. TextView
is not shown up.
Where I am doing wrong or missing something?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试事件的
getParent()
(您的ViewEvent
):try
getParent()
of event (yourViewEvent
):尝试这样。您没有为 TextView 声明任何 id。
然后像下面这样访问这个 TextView。
Try like this.You did not declare any id for your TextView.
Then access this TextView like following.