可重用的视图? [英] Reusable views?
问题描述
我是Android开发的新手,但是我有相当多的Swing / WPF(C#)GUI经验。我想做的是以下事情:
I'm new to Android development, but I have quite some bit of Swing/WPF(C#) GUI experience. What I'm trying to do is the following:
我有一个带有3个独立视图的 BottomNavigationView
。每个视图都是作为片段实现的,该片段通过 OnNavigationItemSelectedListener
在MainActivity Java代码中显示/隐藏:
I have a BottomNavigationView
with 3 separate views. Each view is implemented as a fragment that is shown/ hidden in the MainActivity java code through a OnNavigationItemSelectedListener
:
private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
= new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.one:
findViewById(R.id.one).setVisibility(View.VISIBLE);
findViewById(R.id.two).setVisibility(View.GONE);
findViewById(R.id.threee).setVisibility(View.GONE);
return true;
// ... and so on
我的问题是接口的一部分其中两个片段是相同的,由一组 EditText
和 ImageButton
组成。因此,我想到了创建可重用的东西,而不是将代码加倍。我的选择是在片段内部制作一个片段(但在互联网上我认为这是不建议的)或制作可重复使用的视图( https://developer.android.com/training/improving-layouts/reusing-layouts.html ,> https://developer.android.com/guide/topics/ui/custom-components.html )。最好的方法是什么?我想要一个带有已连接Java代码的xml布局,在这里我可以收听 EditText
和 Buttons
并采取相应行动。也许我对WPF的经验有偏见,但我试图使其像xaml +代码隐藏,这可能不是构建Android应用程序时解决问题的正确方法。
My problem is that a part of the interface of two of the fragments is identical and made of a set of EditText
and ImageButton
. So I thought of creating something re-usable instead of doubling the code. My options are to make a fragment inside a fragment (but around the internet I saw that this is not advisable) or to make a re-usable view (https://developer.android.com/training/improving-layouts/reusing-layouts.html, https://developer.android.com/guide/topics/ui/custom-components.html). What is the best way to do it? I'd like to have a xml layout with a connected java code, where I can listen to the interactions with the EditText
and the Buttons
and act accordingly. Maybe I'm biased by my WPF experience but I'm trying to make it like a xaml + codebehind, which may not be the correct way to tackle the problem when building Android apps.
推荐答案
您将必须为视图定义一个单独的XML布局,该视图将多次使用,并且
You will have to define a separate XML layout for a View that you will use more than once and
在XML中
您将在布局内使用< include>
标记,例如
...
<include layout="@layout/your_frequent_used_layout"/>
<TextView android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
android:padding="10dp" />
....
更多信息此处为文档培训!
在Java中
对于Java端如果要获取特定视图并添加到当前布局,则需要布局Inflater,然后将其添加到在活动中像这样查看
(在您的片段中考虑是否抛出任何错误)
第一个选项:
View view = null;
LayoutInflater inflater =
(LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.your_reusable_view, null);
your_main_root_view.addView(view);
第二个选项:
View child = getLayoutInflater().inflate(R.layout.your_reusable_view, null);
your_main_root_view.addView(child);
这篇关于可重用的视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!