安卓组件setContentView会清除原先的组件,但是我有一个组件是希望长期保留的并且显示在最上层,这个怎么办?

您所在位置: &
&nbsp&&nbsp&nbsp&&nbsp
基于Android平台的天气查询系统的设计与开发安卓天气预报毕业论文精选整理.doc 50页
本文档一共被下载:
次 ,您可全文免费在线阅读后下载本文档。
下载提示
1.本站不保证该用户上传的文档完整性,不预览、不比对内容而直接下载产生的反悔问题本站不予受理。
2.该文档所得收入(下载+内容+预览三)归上传者、原创者。
3.登录后可充值,立即自动返金币,充值渠道很便利
基于Android平台的天气查询系统的设计与开发安卓天气预报毕业论文精选整理
你可能关注的文档:
··········
··········
本科毕业论文(设计)
基于Android平台的天气查询系统的设计与开发
天气预报是非常实用的信息服务,随着移动设备的性能不断提高,天气预报了人们日常生活水平的提高,需要访问预报解析天气,未来~5天的天气预报及
关键字:数据接口Android
The weather forecast is very practical information service, with the continuous improvement of the performance of mobile devices, weather forecast under the impetus of the continuous progress of science and technology, has realized the centralized and unified, digital services. With the improvement of people's daily living standards, it is urgent to need efficient, timely and stable weather interactive platform. Therefore, it is necessary to realize the information system of a weather forecast service function.. That saves time, but also can achieve personalized service, reduce unnecessary waiting and heavy information, convenient users to understand the latest weather information.
By visiting the polymerization data the weather data interface, using httpclient to get communication mode, to establish a network connection, use httpget to read the data and through the httpresponse acquiring entity return value, is introduced in this paper. And resolved to get real-time weather, weather forecasts and life information of the next 4~5 days. Users can choose the city to get the weather information including wind, wind direction, UV intensity and so on. Greatly convenient for the user itinerary or other circumstances, to avoid unnecessary trouble, and the interface is beautiful, simple operation, with strong practicality.
Keywords: API; UI; Real-t Android
Abstract II
研究背景 1
国内外的研究状况及发展趋势 1
系统分析及其主要内容 2
正在加载中,请稍后...Android View系统分析之从setContentView说开来(一)
今天是中秋节,先祝各位中秋快乐吧。作为北漂的人,对于过节最大的感触就是没气氛~ 中秋是一个特别重要的节日,小的时候过中秋都是特别快乐的,有月饼吃,和家人上月,过完中秋要去亲戚家拜访等等。现在对于我们来说也就是一个节日罢了,窝在家里看点电视、看点书、吃顿好的,虽说生活好了,但日子过得没啥滋味。废话不多说,开始今天的学习吧。Hello World
对于学习的人而言,大多数人第一个项目都是著名的"Hello World",自从K&R开了这个先例,后面的人就很少有打破的。学习开发也是这样,我们第一次创建应用,估计也就是运行程序,然后在模拟器上输出一个Hello World,我们看到最简单的Activity中的内容大致是这样的:public class MainActivity extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
}main_activity.xml大致是这样的 :
然后执行程序,我们就可以看到模拟器中的Hello World了。
我们在整个过程中做的事情很少,在我们的main_activity.xml我们只有一个显示文本的TextView,但是在上图中却还多了一个title。我们好奇的是整个过程是怎么工作的?对于大型系统来说细节总是复杂的,在下水平有限,所以我们今天只来理一下它的基本脉络。setContentView 一般来说我们设置页面的内容视图是都是通过setContentView方法,那么我们就以2.3源码为例就来看看Activity中的setContentView到底做了什么吧。vcD48cD48L3A+PHByZSBjbGFzcz0="brush:">
* Set the activity content from a layout resource.
The resource will be
* inflated, adding all top-level views to the activity.
* @param layoutResID Resource ID to be inflated.
public void setContentView(int layoutResID) {
getWindow().setContentView(layoutResID);
public Window getWindow() {
private Window mW
我们可以看到,实际上调用的mWindow的setContentView方法,在Android Touch事件分发过程这篇文章中我们已经指出Window的实现类为PhoneWindow类,我们就移步到PhoneWindow的setConentView吧,核心如下 :
public void setContentView(int layoutResID) {
if (mContentParent == null) {
installDecor();
// 1、生成DecorView
mContentParent.removeAllViews();
mLayoutInflater.inflate(layoutResID, mContentParent);// 2、将layoutResId的布局添加到mContentParent中
final Callback cb = getCallback();
if (cb != null) {
cb.onContentChanged();
// 构建mDecor对象,并且初始化标题栏和Content Parent(我们要显示的内容区域)
private void installDecor() {
if (mDecor == null) {
mDecor = generateDecor();
// 3、构建DecorView
mDecor.setDescendantFocusability(ViewGroup.FOCUS_AFTER_DESCENDANTS);
mDecor.setIsRootNamespace(true);
if (mContentParent == null) {
mContentParent = generateLayout(mDecor);
// 4、获取ContentView容器,即显示内容的区域
mTitleView = (TextView)findViewById(com.android.internal.R.id.title); 5、设置Title等
if (mTitleView != null) {
if ((getLocalFeatures() & (1 << FEATURE_NO_TITLE)) != 0) {
View titleContainer = findViewById(com.android.internal.R.id.title_container);
if (titleContainer != null) {
titleContainer.setVisibility(View.GONE);
mTitleView.setVisibility(View.GONE);
if (mContentParent instanceof FrameLayout) {
((FrameLayout)mContentParent).setForeground(null);
mTitleView.setText(mTitle);
protected DecorView generateDecor() {
return new DecorView(getContext(), -1);
// 构建mDecor对象
} 我们可以看到,setContentView的基本流程简单概括就是如下几步:1、构建mDecor对象。mDecor就是整个窗口的顶层视图,它主要包含了Title和Content View两个区域 (参考图1中的两个区域 ),Title区域就是我们的标题栏,Content View区域就是显示我们xml布局内容中的区域。关于mDecor对象更多说明也请参考Android Touch事件分发过程这篇文章;2、设置一些关于窗口的属性,初始化标题栏区域和内容显示区域; 这里比较复杂的就是generateLayout(mDecor)这个函数,我们一起来分析一下吧。
// 返回用于显示我们设置的页面内容的ViewGroup容器
protected ViewGroup generateLayout(DecorView decor) {
// Apply data from current theme.
// 1、获取窗口的Style属性
TypedArray a = getWindowStyle();
if (false) {
System.out.println("From style:");
String s = "Attrs:";
for (int i = 0; i < com.android.internal.R.styleable.Window. i++) {
s = s + " " + Integer.toHexString(com.android.internal.R.styleable.Window[i]) + "="
+ a.getString(i);
System.out.println(s);
// 窗口是否是浮动的
mIsFloating = a.getBoolean(com.android.internal.R.styleable.Window_windowIsFloating, false);
int flagsToUpdate = (FLAG_LAYOUT_IN_SCREEN|FLAG_LAYOUT_INSET_DECOR)
& (~getForcedWindowFlags());
if (mIsFloating) {
setLayout(WRAP_CONTENT, WRAP_CONTENT);
setFlags(0, flagsToUpdate);
setFlags(FLAG_LAYOUT_IN_SCREEN|FLAG_LAYOUT_INSET_DECOR, flagsToUpdate);
// 设置是否不显示title区域
if (a.getBoolean(com.android.internal.R.styleable.Window_windowNoTitle, false)) {
requestFeature(FEATURE_NO_TITLE);
// 设置全屏的flag
if (a.getBoolean(com.android.internal.R.styleable.Window_windowFullscreen, false)) {
setFlags(FLAG_FULLSCREEN, FLAG_FULLSCREEN&(~getForcedWindowFlags()));
if (a.getBoolean(com.android.internal.R.styleable.Window_windowShowWallpaper, false)) {
setFlags(FLAG_SHOW_WALLPAPER, FLAG_SHOW_WALLPAPER&(~getForcedWindowFlags()));
WindowManager.LayoutParams params = getAttributes();
// 设置输入法模式
if (!hasSoftInputMode()) {
params.softInputMode = a.getInt(
com.android.internal.R.styleable.Window_windowSoftInputMode,
params.softInputMode);
if (a.getBoolean(com.android.internal.R.styleable.Window_backgroundDimEnabled,
mIsFloating)) {
/* All dialogs should have the window dimmed */
if ((getForcedWindowFlags()&WindowManager.LayoutParams.FLAG_DIM_BEHIND) == 0) {
params.flags |= WindowManager.LayoutParams.FLAG_DIM_BEHIND;
params.dimAmount = a.getFloat(
android.R.styleable.Window_backgroundDimAmount, 0.5f);
// 窗口动画
if (params.windowAnimations == 0) {
params.windowAnimations = a.getResourceId(
com.android.internal.R.styleable.Window_windowAnimationStyle, 0);
// The rest are only done if this wi otherwise,
// the values are inherited from our container.
if (getContainer() == null) {
if (mBackgroundDrawable == null) {
if (mBackgroundResource == 0) {
mBackgroundResource = a.getResourceId(
com.android.internal.R.styleable.Window_windowBackground, 0);
if (mFrameResource == 0) {
mFrameResource = a.getResourceId(com.android.internal.R.styleable.Window_windowFrame, 0);
if (false) {
System.out.println("Background: "
+ Integer.toHexString(mBackgroundResource) + " Frame: "
+ Integer.toHexString(mFrameResource));
mTextColor = a.getColor(com.android.internal.R.styleable.Window_textColor, 0xFF000000);
// Inflate the window decor.
// 2、根据一些属性来选择不同的顶层视图布局,例如设置了FEATURE_NO_TITLE的属性,那么就选择没有Title区域的那么布局;
// layoutResource布局就是整个Activity的布局,其中含有title区域和content区域,content区域就是用来显示我通过
// setContentView设置进来的内容区域,也就是我们要显示的视图。
int layoutR
int features = getLocalFeatures();
// System.out.println("Features: 0x" + Integer.toHexString(features));
if ((features & ((1 << FEATURE_LEFT_ICON) | (1 << FEATURE_RIGHT_ICON))) != 0) {
if (mIsFloating) {
layoutResource = com.android.internal.R.layout.dialog_title_
layoutResource = com.android.internal.R.layout.screen_title_
// System.out.println("Title Icons!");
} else if ((features & ((1 << FEATURE_PROGRESS) | (1 << FEATURE_INDETERMINATE_PROGRESS))) != 0) {
// Special case for a window with only a progress bar (and title).
// XXX Need to have a no-title version of embedded windows.
layoutResource = com.android.internal.R.layout.screen_
// System.out.println("Progress!");
} else if ((features & (1 << FEATURE_CUSTOM_TITLE)) != 0) {
// Special case for a window with a custom title.
// If the window is floating, we need a dialog layout
if (mIsFloating) {
layoutResource = com.android.internal.R.layout.dialog_custom_
layoutResource = com.android.internal.R.layout.screen_custom_
} else if ((features & (1 << FEATURE_NO_TITLE)) == 0) {
// If no other features and not embedded, only need a title.
// If the window is floating, we need a dialog layout
if (mIsFloating) {
layoutResource = com.android.internal.R.layout.dialog_
layoutResource = com.android.internal.R.layout.screen_
// System.out.println("Title!");
// Embedded, so no decoration is needed.
layoutResource = com.android.internal.R.layout.screen_
// System.out.println("Simple!");
mDecor.startChanging();
// 3、加载视图
View in = mLayoutInflater.inflate(layoutResource, null);
// 4、将layoutResource的内容添加到mDecor中
decor.addView(in, new ViewGroup.LayoutParams(MATCH_PARENT, MATCH_PARENT));
// 5、获取到我们的内容显示区域,这是一个ViewGroup类型的,其实是FrameLayout
ViewGroup contentParent = (ViewGroup)findViewById(ID_ANDROID_CONTENT);
if (contentParent == null) {
throw new RuntimeException("Window couldn't find content container view");
if ((features & (1 << FEATURE_INDETERMINATE_PROGRESS)) != 0) {
ProgressBar progress = getCircularProgressBar(false);
if (progress != null) {
progress.setIndeterminate(true);
// 6、设置一些背景、title等属性
// Remaining setup -- of background and title -- that only applies
// to top-level windows.
if (getContainer() == null) {
Drawable drawable = mBackgroundD
if (mBackgroundResource != 0) {
drawable = getContext().getResources().getDrawable(mBackgroundResource);
mDecor.setWindowBackground(drawable);
drawable =
if (mFrameResource != 0) {
drawable = getContext().getResources().getDrawable(mFrameResource);
mDecor.setWindowFrame(drawable);
// System.out.println("Text=" + Integer.toHexString(mTextColor) +
// " Sel=" + Integer.toHexString(mTextSelectedColor) +
// " Title=" + Integer.toHexString(mTitleColor));
if (mTitleColor == 0) {
mTitleColor = mTextC
if (mTitle != null) {
setTitle(mTitle);
setTitleColor(mTitleColor);
mDecor.finishChanging();
return contentP
} 其实也就是这么几个步骤:1、获取用户设置的一些属性与Flag;2、根据一些属性选择不同的顶层视图布局,例如FEATURE_NO_TITLE则选择没有title的布局文件等;这里我们看一个与图1中符合的顶层布局吧,即layoutResource = com.android.internal.R.layout.screen_title的情形:
&frameLayout
android:layout_width="match_parent"
android:layout_height="?android:attr/windowTitleSize"
style="?android:attr/windowTitleBackgroundStyle"&
&/frameLayout&
&frameLayout android:id="@android:id/content"
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:foregroundGravity="fill_horizontal|top"
android:foreground="?android:attr/windowContentOverlay" /&
我们可以看到有两个区域,即title区域和content区域,generateLayout函数中的 // 5、获取到我们的内容显示区域,这是一个ViewGroup类型的,其实是FrameLayout
ViewGroup contentParent = (ViewGroup)findViewById(ID_ANDROID_CONTENT);获取的就是xml中id为content的FrameLayout,这个content就是我们的内容显示区域。整个布局对应的效果如下 :
这两个区域就组成了mDecor视图,我们的main_activity.xml就是放在内容视图这个区域的。3、加载顶层布局文件,转换为View,将其添加到mDecor中;4、获取内容容器Content Parent,即用于显示我们的内容的区域;5、设置一些背景图和title等。 在经过这几步,我们就得到了mContentParent,这就是用来装载我们的视图的ViewGroup。再回过头来看setContentView函数:
public void setContentView(int layoutResID) {
if (mContentParent == null) {
installDecor();
// 1、生成DecorView,并且根据窗口属性加载顶级视图布局、获取mContentParent、设置一些基本属性等
mContentParent.removeAllViews();
mLayoutInflater.inflate(layoutResID, mContentParent);// 2、将layoutResId加载到mContentParent中,这里的layoutResId就是我们的main_activity.xml
final Callback cb = getCallback();
if (cb != null) {
cb.onContentChanged();
} 我们看看LayoutInflater的inflate函数吧 :
* Inflate a new view hierarchy from the specified xml resource. Throws
* {@link InflateException} if there is an error.
* @param resource ID for an XML layout resource to load (e.g.,
R.layout.main_page)
* @param root Optional view to be the parent of the generated hierarchy.
* @return The root View of the inflated hierarchy. If root was supplied,
this is the root V otherwise it is the root of the inflated
public View inflate(int resource, ViewGroup root) {
return inflate(resource, root, root != null);
* Inflate a new view hierarchy from the specified xml resource. Throws
* {@link InflateException} if there is an error.
* @param resource ID for an XML layout resource to load (e.g.,
R.layout.main_page)
* @param root Optional view to be the parent of the generated hierarchy (if
attachToRoot is true), or else simply an object that
provides a set of LayoutParams values for root of the returned
hierarchy (if attachToRoot is false.)
* @param attachToRoot Whether the inflated hierarchy should be attached to
the root parameter? If false, root is only used to create the
correct subclass of LayoutParams for the root view in the XML.
* @return The root View of the inflated hierarchy. If root was supplied and
attachToRoot is true, otherwise it is the root of
the inflated XML file.
public View inflate(int resource, ViewGroup root, boolean attachToRoot) {
if (DEBUG) System.out.println("INFLATING from resource: " + resource);
XmlResourceParser parser = getContext().getResources().getLayout(resource);
return inflate(parser, root, attachToRoot);
} finally {
parser.close();
实际上就是将layoutResId这个布局的视图附加到mContentParent中。DecorView 移步 : DecorView 。ViewGroup ViewGroup从语义上来说就是视图组,它也继承自View类,它其实就是视图的容器。我们看官方的定义 :
* A ViewGroup is a special view that can contain other views
* (called children.) The view group is the base class for layouts and views
* containers. This class also defines the
* {@link android.view.ViewGroup.LayoutParams} class which serves as the base
* class for layouts parameters. 我们通过ViewGroup来组织、管理子视图,例如我们常见的FrameLayout、LinearLayout、RelativeLayout、ListView等都是ViewGroup类型,总之只要能包含其他View或者ViewGroup的都是ViewGroup类型。使用ViewGroup来构建视图树。View View就是UI界面上的一个可见的组件,任何在UI上可见的都为View的子类。我们看官方定义 : * This class represents the basic building block for user interface components. A View
* occupies a rectangular area on the screen and is responsible for drawing and
* event handling. View is the base class for widgets, which are
* used to create interactive UI components (buttons, text fields, etc.). The
* {@link android.view.ViewGroup} subclass is the base class for layouts, which
* are invisible containers that hold other Views (or other ViewGroups) and define
* their layout properties. TextView、Button、ImageView、FrameLayout、LinearLayout、ListView等都是View的子类。总结
整个窗口由Title区域和Content区域组成,Content区域就是我们要显示内容的区域,在这个区域中mContentParent是根ViewGroup,由mContentParent组织、管理其子视图,从而构建整个视图树。当Activity启动时,就将这些内容就会显示在手机上。1、setContentView的作用是将View加载到根view之上,这样当显示view时,先显示根view,然后在显示子view,以此类推,最终将所有view显示出来。
2、setContentView必须要放在findviewbyid之前,因为view在加载之前是无法引用的。
3、setContentView最本质的作用是为要显示的view分配内存。
4、activity、window和view之间的关系:
而当我们运行程序的时候,有一个setContentView()方法,Activity其实不是显示视图(直观上感觉是它),实际上Activity调用了PhoneWindow的setContentView()方法,然后加载视图,将视图放到这个Window上,而Activity其实构造的时候初始化的是Window(PhoneWindow),Activity其实是个控制单元,即可视的人机交互界面。
打个比喻:
Activity是一个工人,它来控制Window;Window是一面显示屏,用来显示信息;View就是要显示在显示屏上的信息,这些View都是层层重叠在一起(通过infalte()和addView())放到Window显示屏上的。而LayoutInfalter就是用来生成View的一个工具,XML布局文件就是用来生成View的原料
activity调用setContentView其实是调用window的方法
在activity中:
public void setContentView(int layoutResID) {
getWindow().setContentView(layoutResID);
initActionBar();
在PhoneWindow类中:
public void setContentView(int layoutResID) {
if (mContentParent == null) {
installDecor();
mContentParent.removeAllViews();
mLayoutInflater.inflate(layoutResID, mContentParent);
final Callback cb = getCallback();
if (cb != null && !isDestroyed()) {
cb.onContentChanged();
从上面代码可以看出,最终的根View就是mContentParent,而mContentParent就是installDecor()生成的DecorView。
5、如何在一个activity中显示不同布局?
1)使用LayoutInflater inflate两个view:v1,v2
2)使用setContentView(View view)方法设置这两个view
从setContentView说起
从我们学习Android开发的第一天开始,我们就知道在Activity#onCreate里调用setContentView,Activity就会根据XML布局文件来显示。那么setContentVie...
setContentView和inflate区别
setContentView和inflate区别
一般用LayoutInflater做一件事:inflate
inflate这个方法总共有四种形式(见下面),目的都是把xml表述的layout...
setContentView到底做了什么
对Activity的setContentView的使用,大家比较熟悉了,当然,对setContentView的原理估计也比较熟悉,网上有不少的文章,不过,还是写一篇这方面的东西,记录下,毕竟很多东西,...
setContentView剖析
1、setContentView的作用是将View加载到根view之上,这样当显示view时,先显示根view,然后在显示子view,以此类推,最终将所有view显示出来。
2、setCon...
addContentView和setContentView的区别
两者的区别主要包括两点:
以添加UI组件是否被移除
setContentView() 会导致先前添加的被移除, 即替换性的;
而 addContentView() 不会移除先前添加的UI...
安卓-setContentView(R.layout.main)意义和作用
setContentView(R.layout.main)在Android里面,这句话是什么意思?
牛兒子 | 浏览
24260 次 |举报
我有更好的答案
相当于显示所指定的界面
...
浅谈Activity中setContentView()
PhoneWindow类中相关代码解读
installDecor
generateLayout
总结一下引言今天来研究一下Android中setContentView()方法的具体实现。...
Android中不得不谈的setContentView
本文原创,转载请注明出处。
欢迎关注我的 简书 ,关注我的专题 Android Class 我会长期坚持为大家收录简书上高质量的Android相关博文。
写在前面:几个月之前在做项目的布局优化时...
setContentView
1、setContentView的作用是将View加载到根view之上,这样当显示view时,先显示根view,然后在显示子view,以此类推,最终将所有view显示出来。
android之setContentView,addContentView(),Window,WindowManager,Dialog源码剖析。
setContenView:
任何一个Activity在onCreat()方法里要执行一次setContentView,而setContentView作用笔者总结为两大类
ONE:第一次setC...
没有更多推荐了,java - cannot resolve symbol R, setContentView() android studio - Stack Overflow
to customize your list.
This site uses cookies to deliver our services and to show you relevant ads and job listings.
By using our site, you acknowledge that you have read and understand our , , and our .
Your use of Stack Overflow’s Products and Services, including the Stack Overflow Network, is subject to these policies and terms.
Join Stack Overflow to learn, share knowledge, and build your career.
or sign in with
I have found numerous posts about this issue and read through them. I have tried rebuilding the project, do a project clean and a gradle-clean as well still didn't help.
Perhaps there's something wrong with my codes?
The error I get lies in "setContentView(R.layout.read_comments)" cannot resolve symbol R
Here is my code:
package com.example.
import android.app.A
import android.os.B
public class ReadComments extends Activity{
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.read_comments);
This error sometimes happened if there is error in one of your XML layout files. In this case, the project failed to generate the R file.
Check the other errors which appears to you to know which error and in which xml layout file caused this.
If the project was working before and it is not working now, check the project history to know which modification caused this error.
I agree, the problem is elsewhere in the project, check the Problems tab (I'm using Eclipse - Juno version).
I had created the project using an earlier version of Eclipse, after importing the project into the new version of Eclipse it modified the project file from
target=Google Inc.:Google APIs:15
target=Google Inc.:Google APIs:10
Once I reverted back to 15 the R errors resolved themselves.
I had the same issue and as Mina Tadros pointed out you should look to the error log for some direction. In my case it pointed me to an EditText field that I simply deleted and rewrote and the problem was fixed.
Your Answer
Sign up or
Sign up using Google
Sign up using Facebook
Post as a guest
Post as a guest
Post Your Answer
By clicking &Post Your Answer&, you acknowledge that you have read our updated ,
and , and that your continued use of the website is subject to these policies.
Not the answer you&#39;re looking for?
Browse other questions tagged
Stack Overflow works best with JavaScript enabled

我要回帖

更多关于 安卓4大组件 的文章

 

随机推荐