我创建了一个简单的Android Studio应用来练习和了解更多信息。但是XML中的元素在模拟器中不可见。我该如何解决这个问题?
我正在研究,但没有找到答案。
XML布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity"
android:padding="16dp">
<TextView
android:id="@+id/text_welcome"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/text_welcome"
android:textSize="26sp"/>
<TextView
android:id="@+id/text_instructions"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:text="@string/text_instruction"
android:textSize="20sp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:gravity="center">
<EditText
android:id="@+id/edit_feet"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="4"
android:hint="@string/edit_feet"
android:inputType="number" />
<EditText
android:id="@+id/edit_inchee"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="12dp"
android:ems="4"
android:hint="@string/edit_inches"
android:inputType="number" />
<EditText
android:id="@+id/edit_pounds"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="12dp"
android:ems="4"
android:hint="@string/edit_pounds"
android:inputType="number" />
</LinearLayout>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/boutton_calculate_bmi"
android:text="@string/boutton_cal_bmi"/>
<TextView
android:id="@+id/text_result"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:text="@string/text_result"
android:textSize="18sp" />
</LinearLayout>
活动代码:
package com.applike.bmi;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import org.w3c.dom.Text;
public class MainActivity extends AppCompatActivity {
TextView text_result;
EditText edit_feet , edit_inches , edit_pounds;
Button button_calculate_bmi;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/*
edit_feet = (EditText)findViewById(R.id.edit_feet);
edit_inches = (EditText)findViewById(R.id.edit_inchee);
edit_pounds = (EditText)findViewById(R.id.edit_pounds);
button_calculate_bmi = (Button)findViewById(R.id.boutton_calculate_bmi);
text_result = (TextView)findViewById(R.id.text_result);
button_calculate_bmi.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String results="Results: ";
// int feet = Integer.parseInt(edit_feet.getText().toString());
if(edit_feet.getText().length() == 0){
edit_feet.setError("Please enter a #.");
results += "Oops! There was an error. ";
}
else if(edit_feet.getText().length() > 1){
edit_feet.setError("Too many digits");
results += "Our records indicate that your cannot possibily" +
" be thay tall. ";
}
else {
results += "The length of feet is " +
edit_feet.getText().length();
}
text_result.setText(results);
}
});
*/
}
}
我的XML文件有问题吗? 请为此提供解决方案。我该如何处理?
答案 0 :(得分:2)
您不会在“活动”中夸大您的布局,因此也就不会显示它。您需要使用setContentView()
中的onCreate()
函数来扩大布局,如下所示:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
...
}
答案 1 :(得分:0)
您需要使用 onCreate()方法设置布局,如下所示:
setContentView(R.layout.activity_main_new);
答案 2 :(得分:0)
您不会在Activity中扩大布局。您需要使用onCreate()中的setContentView()方法来扩大布局,以便它在设备上可见:
setContentView(R.layout.activity_main_new); //activity_main_new defines your xml layout.
如果要在Fragment中增加布局。您需要在Fragment的onCreateView()方法中定义布局:
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.homecredit, container, false); //where home credit is your layout file, container is root layout and attachToRoot is a boolean value
}