简体   繁体   English

多布局多视图 Android Studio

[英]Multiple layouts and multiple views Android Studio

I am trying to build an app and I want to add multiple views in my app.我正在尝试构建一个应用程序,我想在我的应用程序中添加多个视图。 I am struggling to find a way to handle the views properly.我正在努力寻找一种正确处理视图的方法。 If you do it within one class (MainActivity) you need to do some hiding, which I think is not that neet and can be done in a more solid way, but I do not know how.如果您在一个类 (MainActivity) 中执行此操作,则需要进行一些隐藏,我认为这不是那么好,可以以更可靠的方式完成,但我不知道如何进行。

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
    StrictMode.setThreadPolicy(policy);


    editTextAddress = (EditText) findViewById(R.id.address);
    editTextPort = (EditText) findViewById(R.id.port);
    editTextMsg = (EditText) findViewById(R.id.msgtosend);
    buttonCalibrate = (Button)findViewById(R.id.calibrate);
    buttonMove0 = (Button)findViewById(R.id.Move0);
    buttonConnect = (Button) findViewById(R.id.connect);
    buttonDisconnect = (Button) findViewById(R.id.disconnect);
    buttonSend = (Button)findViewById(R.id.send);
    textViewState = (TextView)findViewById(R.id.state);
    textViewRx = (TextView)findViewById(R.id.received);


    buttonDisconnect.setEnabled(false);
    buttonSend.setEnabled(false);

    buttonConnect.setOnClickListener(buttonConnectOnClickListener);
    buttonDisconnect.setOnClickListener(buttonDisConnectOnClickListener);
    buttonSend.setOnClickListener(buttonSendOnClickListener);
    buttonMove0.setOnClickListener(buttonMove0OnClickListener);
    buttonCalibrate.setOnClickListener(buttonCalibrateOnClickListener);

For example this is my java code, some of these buttons are not from activity_main.xml but from another xml file.例如,这是我的 java 代码,其中一些按钮不是来自 activity_main.xml,而是来自另一个 xml 文件。 The app just returns a NULL pointer with those buttons, which is logical.该应用程序仅返回带有这些按钮的 NULL 指针,这是合乎逻辑的。

I have seen many ways but I cannot decide which one is the proper one.我见过很多方法,但我无法决定哪一种是合适的。 What is the proper way?什么是正确的方法?

you can use butterKnife to help you in this and you can simply do this你可以使用butterKnife来帮助你,你可以简单地做到这一点

public class MainActivity extends Activity {  
@BindView(R.id.sample_text) TextView textView;
@BindView(R.id.sample_button) Button button;

@Override 
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
textView.setText("You can change this view accordingly");

@OnClick(R.id.click_button) 
void buttonClick() {  
//..you don't even need the line @InjectView(R.id.click_button) if this 
button isn't being used else where
}

}
}

If your want to show another layout in your activity layout you need to include that layout如果您想在您的活动布局中显示另一个布局,您需要include该布局

Including another_layout.xml in activity_main.xml layoutactivity_main.xml布局中包含another_layout.xml

...
<include
    android:id="@+id/included_layout"
    layout="@layout/another_layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="0.1"
    android:gravity="bottom"/>
....

this code will set the layout of the bottom of main layout.此代码将设置主布局底部的布局。

You can hide this layout as you wish您可以根据需要隐藏此布局

View view = (View) findViewById(R.id.included_layout);
view.setVisibility(View.INVISIBLE);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM