java - 为什么使用以下 android 代码看不到布局组件?
问题描述
我已将以下 XML 代码用于 android 布局,但在运行(或安装)代码后 EditText 不可见。
XML 代码:activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:padding="10dp"
tools:context=".MainActivity">
<TextView
android:id="@+id/text_view_show_uploads"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_marginStart="30dp"
android:layout_marginLeft="30dp"
android:layout_marginEnd="30dp"
android:layout_marginRight="30dp"
android:gravity="center"
android:text="Upload Products Images"
android:textColor="#f00fff"
android:textSize="25sp" />
<Button
android:id="@+id/button_choose_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/text_view_show_uploads"
android:layout_marginTop="50dp"
android:text="Choose file" />
<Spinner
android:id="@+id/spinner2"
android:layout_width="231dp"
android:layout_height="45dp"
android:layout_below="@id/text_view_show_uploads"
android:layout_marginStart="16dp"
android:layout_marginLeft="16dp"
android:layout_marginTop="50dp"
android:layout_toEndOf="@+id/button_choose_image"
android:layout_toRightOf="@+id/button_choose_image" />
<ImageView
android:id="@+id/image_view1"
android:layout_width="180dp"
android:layout_height="160dp"
android:layout_above="@id/progress_bar"
android:layout_below="@id/spinner2"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_marginLeft="10dp"
android:layout_marginTop="12dp"
android:layout_marginEnd="210dp"
android:layout_marginRight="210dp"
android:layout_marginBottom="331dp" />
<ImageView
android:id="@+id/image_view2"
android:layout_width="180dp"
android:layout_height="160dp"
android:layout_above="@id/progress_bar"
android:layout_below="@id/spinner2"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_marginLeft="10dp"
android:layout_marginTop="172dp"
android:layout_marginEnd="210dp"
android:layout_marginRight="210dp"
android:layout_marginBottom="168dp" />
<ImageView
android:id="@+id/image_view3"
android:layout_width="180dp"
android:layout_height="160dp"
android:layout_above="@id/progress_bar"
android:layout_below="@id/spinner2"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_marginLeft="10dp"
android:layout_marginTop="335dp"
android:layout_marginEnd="210dp"
android:layout_marginRight="210dp"
android:layout_marginBottom="5dp" />
<ImageView
android:id="@+id/image_view4"
android:layout_width="180dp"
android:layout_height="160dp"
android:layout_above="@id/progress_bar"
android:layout_below="@id/spinner2"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_marginLeft="10dp"
android:layout_marginTop="12dp"
android:layout_marginEnd="-2dp"
android:layout_marginRight="-2dp"
android:layout_marginBottom="331dp" />
<ImageView
android:id="@+id/image_view5"
android:layout_width="180dp"
android:layout_height="160dp"
android:layout_above="@id/progress_bar"
android:layout_below="@id/spinner2"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_marginLeft="10dp"
android:layout_marginTop="172dp"
android:layout_marginEnd="-2dp"
android:layout_marginRight="-2dp"
android:layout_marginBottom="168dp" />
<ImageView
android:id="@+id/image_view6"
android:layout_width="180dp"
android:layout_height="160dp"
android:layout_above="@id/progress_bar"
android:layout_below="@id/spinner2"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_marginLeft="10dp"
android:layout_marginTop="335dp"
android:layout_marginEnd="-2dp"
android:layout_marginRight="-2dp"
android:layout_marginBottom="5dp" />
<ProgressBar
android:id="@+id/progress_bar"
style="@style/Widget.AppCompat.ProgressBar.Horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="@+id/button_upload"
android:layout_marginBottom="16dp" />
<Button
android:id="@+id/button_upload"
android:layout_width="149dp"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:layout_marginStart="60dp"
android:layout_marginLeft="60dp"
android:layout_marginRight="10dp"
android:layout_marginBottom="13dp"
android:layout_toEndOf="@+id/edit_ID_name"
android:layout_toRightOf="@+id/edit_ID_name"
android:text="Upload" />
<EditText
android:id="@+id/edit_ID_name"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_below="@id/text_view_show_uploads"
android:layout_marginLeft="10dp"
android:layout_marginTop="611dp"
android:layout_marginBottom="7dp"
android:hint="Enter Product Id" />
</RelativeLayout>
最后一个布局组件(EditText)在运行后不可见。相关java代码:MainActivity.java
package com.example.image_view_from_gallery;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
Button mButtonChooseImage;
ImageView mImageView1;
ImageView mImageView2;
ImageView mImageView3;
ImageView mImageView4;
ImageView mImageView5;
ImageView mImageView6;
int PICK_IMAGE_MULTIPLE = 1;
TextView total;
ArrayList<Uri> uris;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mButtonChooseImage = findViewById(R.id.button_choose_image);
total = findViewById(R.id.text);
mImageView1 = findViewById(R.id.image_view1);
mImageView2 = findViewById(R.id.image_view2);
mImageView3 = findViewById(R.id.image_view3);
mImageView4 = findViewById(R.id.image_view4);
mImageView5 = findViewById(R.id.image_view5);
mImageView6 = findViewById(R.id.image_view6);
uris = new ArrayList<Uri>();
mButtonChooseImage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) { // initialising intent
Intent intent = new Intent(); // setting type to select to be image
intent.setType("image/*"); // allowing multiple image to be selected
intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_IMAGE_MULTIPLE);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data); // When an Image is picked
if (requestCode == PICK_IMAGE_MULTIPLE && resultCode == RESULT_OK && null != data) { // Get the Image from data
if (data.getClipData() != null) {
// ClipData mClipData = data.getClipData();
int cout = data.getClipData().getItemCount();
for (int i = 0; i < cout; i++) { // adding imageuri in array
Uri imageurl = data.getClipData().getItemAt(i).getUri();
uris.add(imageurl);
} // setting 1st selected image into image switcher
mImageView1.setImageURI(uris.get(0));
mImageView2.setImageURI(uris.get(1));
mImageView3.setImageURI(uris.get(2));
mImageView4.setImageURI(uris.get(3));
mImageView5.setImageURI(uris.get(4));
mImageView6.setImageURI(uris.get(5));
} else {
Uri imageurl = data.getData();
uris.add(imageurl);
mImageView1.setImageURI(uris.get(0));
}
} else {
// show this if no image is selected
Toast.makeText(this, "You haven't picked Image", Toast.LENGTH_LONG).show();
}
}
}
为什么在物理设备上安装后 EditText 不可见?
解决方案
也许是因为android:layout_marginTop="611dp"
在您的 EditText 中。它正在走出屏幕。611dp 的余量太大了。
推荐阅读
- sql - SQL查询从1个表中选择并基于2列返回数据
- c - 为 C 程序指定在同一库中打开 Python 程序的路径的好方法是什么?
- c# - WPF XAML - 文本框的设计时间和可见性
- image - 矩阵的字典排序
- r - 从数据框中计算相似发音词的数量和名称
- reactjs - 如何在 React Native 中打开新屏幕
- python - 使用 MingW 在 Windows 上发布 PyGObject/GTK+ 应用程序
- c++ - 为什么我们需要关闭一个文件来完成一个文件的写入过程?
- django - 试图让 log_error 装饰器在 Django 中工作,但得到元组错误
- python - 使用 Open CV 从线条中提取消失点