[英]Show custom view in all activities
I want to show a view that should be shown in all activities. 我想展示在所有活动中都应显示的视图。 I don't know how to inherit views in android. 我不知道如何在android中继承视图。 What i did is below, its showing the view in first activity but not in all activities. 我所做的是在下面,它显示了第一次活动中的视图,但未显示所有活动中的视图。 This pease of code is form my BaseActivity, please help 此代码是我的BaseActivity的形式,请帮助
LayoutInflater inflater = getLayoutInflater();
View child = inflater.inflate(R.layout.custom_error, null);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams( LayoutParams.WRAP_CONTENT , LayoutParams.WRAP_CONTENT );
params.addRule(RelativeLayout.ALIGN_PARENT_TOP);
addContentView(child, params);
You could get an Android specific View
in the Activity
. 您可以在Activity
获得特定于Android的View
。 For example the following code below will add a TextView
to the Activity
's content area. 例如,下面的以下代码会将TextView
添加到Activity
的内容区域。
TextView tvSample = new TextView(this); tvSample.setText("Hello!"); ((ViewGroup) hostActivity.findViewById(android.R.id.content)).addView(this);
Whereby hostActivity
is your current Activity
and android.R.id.content
is a specific element (the content area, not including the ActionBar
). 其中hostActivity
是您当前的Activity
而android.R.id.content
是特定元素(内容区域,不包括ActionBar
)。
Alternatively, as already stated, make use of <merge>
and <include>
tags in your layout XMLs. 或者,如前所述,在布局XML中使用<merge>
和<include>
标签。
you can do this with two solution for programmatically 1)After adding child view to you parent View need to call setContentView(parentView) and pass you parent layout to it. 您可以通过编程的两种解决方案来实现此目的:1)在将子视图添加到父视图之后,需要调用setContentView(parentView)并将父布局传递给它。 and With XMl 2) You can use include tag. 并且使用XMl 2)您可以使用include标记。 follow this link will help you. 点击此链接将为您提供帮助。 http://developer.android.com/training/improving-layouts/reusing-layouts.html http://developer.android.com/training/improving-layouts/reusing-layouts.html
Have you tried 'include' tag of xml? 您是否尝试过xml的“ include”标签? It will do the job. 它将完成工作。
<include
android:id="@+id/container_header_lyt"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_above=...
android:layout_toLeftOf=...
layout="@layout/header_logo_lyt" //Name of the xml layout file you want to include
/>
In the layout/xxxx use the name of your layout file that should be repeated. 在layout / xxxx中,使用应该重复的布局文件的名称。
After use the above code in your xml file like any other widget. 与其他任何小部件一样,在您的xml文件中使用上述代码之后。
When you want to show it: 当您想显示它时:
FrameLayout rootLayout = (FrameLayout)findViewById(android.R.id.content);
View.inflate(this, R.layout.overlay_layout, rootLayout);
Then when you want to remove it: 然后,当您要删除它时:
FrameLayout rootLayout = (FrameLayout)findViewById(android.R.id.content);
rootLayout.removeViewAt(rootLayout.getChildCount()-1);
That's a concise solution, you should remove the View
by giving the RelativeLayout
an id in the XML file, then remove by: rootLayout.removeView(findViewById(R.id.the_id_of_the_relative_layout));
这是一个简洁的解决方案,您应该通过在XML文件中给RelativeLayout
一个ID来删除View
,然后通过以下rootLayout.removeView(findViewById(R.id.the_id_of_the_relative_layout));
删除: rootLayout.removeView(findViewById(R.id.the_id_of_the_relative_layout));
. 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.