Android 如何将布局视图与活动连接
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2198788/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to connect a layout view with an activity
提问by Andy Jacobs
In my main view, I have:
在我的主要观点中,我有:
public class PlayersActivity extends Activity {
ViewFlipper flipper;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.playercontainer);
flipper = (ViewFlipper) findViewById(R.id.flipper);
}
}
with this view:
有了这个观点:
<ViewFlipper xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/flipper"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<include android:id="@+id/first" layout="@layout/first" />
<include android:id="@+id/second" layout="@layout/playerdetailsview" />
</ViewFlipper>
It displays the first view correctly but I want it to be connected to a java class so I created an FirstActivity class where I can control all my components in the first view but how do I attach the first.xml layout with the FirstActivity java class ?
它正确显示第一个视图,但我希望它连接到一个 java 类,所以我创建了一个 FirstActivity 类,我可以在其中控制第一个视图中的所有组件,但是如何将 first.xml 布局与 FirstActivity java 类附加?
回答by RickNotFred
Say your new xml file is foo.xml
:
假设您的新 xml 文件是foo.xml
:
- Put
foo.xml
file in yourres/layout
directory. - In your new class use
setContentView(R.layout.foo);
- Specify your new class in your manifest file.
- 将
foo.xml
文件放在您的res/layout
目录中。 - 在你的新课程中使用
setContentView(R.layout.foo);
- 在清单文件中指定新类。
See also the topic on declaring layout.
另请参阅有关声明布局的主题。
回答by Mohammad Faisal
1) Create an xml file (say foo.xml
).
2) Put foo.xml
in res/layout
directory.
3) Edit foo.xml
and put some android layout code and save it. e.g.,
1)创建一个xml文件(比如foo.xml
)。
2)将foo.xml
在res/layout
目录中。
3)编辑foo.xml
并放置一些android布局代码并保存。例如,
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ViewFlipper android:id="@+id/viewFlipper1"
android:layout_width="match_parent"
android:layout_height="wrap_content"></ViewFlipper>
</LinearLayout>
4) In your new activity class put
4)在你的新活动课上放
setContentView(R.layout.foo);
For creating an activity see this answer
要创建活动,请参阅此答案
I guess the problem with your xml file is that you had not specified any layout for the activity.
我猜您的 xml 文件的问题在于您没有为活动指定任何布局。
回答by CaptainStony
Not so hard to link 2 layouts just do :
链接 2 个布局并不难,只需执行以下操作:
@Override
public void onClick(View args0) {
setContentView(R.layout.aardelayout);
}
回答by user2046604
Change the name from FirstActivity to firstactivity. Layout is not accept the caps, I was faced the same problem.
将名称从 FirstActivity 更改为 firstactivity。布局不接受大写,我遇到了同样的问题。