【发布时间】:2014-06-09 17:06:44
【问题描述】:
我一直在尝试在 Android 应用上显示两个按钮。但是,这些按钮是不可见的,整个页面都是空白的。这是我的 XML 代码-
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.cameracapture.MainActivity$PlaceholderFragment" >
<Button
android:id="@+id/Capture"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="Capture"
android:text="@string/capture" />
<Button
android:id="@+id/Upload"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="Upload"
android:text="@string/upload" />
<ImageView
android:id="@+id/Display"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:contentDescription="@string/upload"/>
</LinearLayout>
当我在图形布局中查看上述代码时,它完美地显示了按钮。所以,我想这意味着这不是match_parent 或wrap_content 的问题。我什至试图看看引入文本视图是否会有所作为。再次,它完美地显示在图形布局上,但在我的设备上,只有一个空白屏幕可见。
可能是我的设备问题,而不是代码问题?
附:我使用的设备是摩托罗拉 Moto G,它有一个 4.5 英寸的显示屏。
编辑:XML 文件名为 fragment_main。这是我的 JAVA 代码。 包 com.example.cameracapture;
import java.io.File;
import android.support.v7.app.ActionBarActivity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;
import android.provider.MediaStore;
public class MainActivity extends ActionBarActivity {
private static final int CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE = 100;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void Capture(View view){
// create Intent to take a picture and return control to the calling application
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File imagesFolder = new File(Environment.getExternalStorageDirectory(), "DCIM/Camera");
imagesFolder.mkdirs();
File image = new File(imagesFolder, "image_001.jpg");
Uri uriSavedImage = Uri.fromFile(image);// create a file to save the image
intent.putExtra(MediaStore.EXTRA_OUTPUT, uriSavedImage); // set the image file name
// start the image capture Intent
startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
}
public void Upload(View view){
String path = Environment.getExternalStorageDirectory()+ "/DCIM/Camera/image_001.jpg";
File imgFile = new File(path);
if(imgFile.exists())
{
Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
ImageView myImage = (ImageView) findViewById(R.id.Display);
myImage.setImageBitmap(myBitmap);
}
else
Toast.makeText(view.getContext(),"no IMAGE IS PRESENT'",Toast.LENGTH_SHORT).show();
}
}
【问题讨论】:
-
也许您正在以编程方式更改可见性?
-
在您引用此布局的位置发布活动代码。另外,请说明您的布局 xml 的名称
-
检查您是否正在更改 onCreate 方法中按钮的可见性。
-
除非您发布您的 Java 文件,否则 SO 无法帮助您。就您的 XML 而言,它看起来不错。
-
这个
layout file的名称是什么。我认为这个布局在放置架下膨胀了......所以可能就是这个问题......
标签: android