【发布时间】:2019-05-23 03:40:40
【问题描述】:
我创建了一个简单的 Android 工作室应用来练习和了解更多信息。但是 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 文件有问题吗? 请为此提供解决方案。我该如何处理?
【问题讨论】:
-
请检查图片
-
您能否也发布您的
MainActivity.java代码,特别是使布局膨胀的部分? -
尝试使用其他模拟器或设备手机也发
MainActivity.java -
我试过了,但是不行
-
@Omid 以下任何答案对您有帮助吗?
标签: android xml android-layout android-emulator