Android的按钮显示在图形布局,但不是在装置 [英] Android Button shows in graphical layout but not on device
问题描述
我一直在试图对Android应用程序显示两个按钮。但是,按钮不可见,整个页面是一个空白。这是我的XML code -
< LinearLayout中的xmlns:机器人=http://schemas.android.com/apk/res/android
的xmlns:工具=http://schemas.android.com/tools
机器人:layout_width =match_parent
机器人:layout_height =match_parent
机器人:方向=垂直
机器人:paddingBottom会=@扪/ activity_vertical_margin
机器人:paddingLeft =@扪/ activity_horizontal_margin
机器人:paddingRight =@扪/ activity_horizontal_margin
机器人:paddingTop =@扪/ activity_vertical_margin
工具:上下文=$ com.example.cameracapture.MainActivity PlaceholderFragment> <按钮
机器人:ID =@ + ID /捕获
机器人:layout_width =match_parent
机器人:layout_height =WRAP_CONTENT
安卓的onClick =捕捉
机器人:文字=@字符串/捕获/>
<按钮
机器人:ID =@ + ID /上传
机器人:layout_width =match_parent
机器人:layout_height =WRAP_CONTENT
安卓的onClick =上传
机器人:文字=@字符串/上传/>
< ImageView的
机器人:ID =@ + ID /显示器
机器人:layout_width =match_parent
机器人:layout_height =WRAP_CONTENT
机器人:contentDescription =@字符串/上传/>< / LinearLayout中>
当我查看图形布局上面code,它显示了完美的按钮。所以,我想这意味着它不是 match_parent
的问题或 WRAP_CONTENT
。我甚至尝试,看看是否引入文本视图会有所作为。再次,它显示了在图形化布局完美,但我的设备上,只有一个空白屏幕是可见的。
难道是我的设备的问题,而不是code?
P.S。我使用的设备是摩托罗拉摩托G,其中有一块4.5英寸显示屏。
编辑:XML文件被命名为fragment_main。这里是我的JAVA code。
包com.example.cameracapture;
进口的java.io.File;
进口android.support.v7.app.ActionBarActivity;
进口android.content.Intent;
进口android.graphics.Bitmap;
进口android.graphics.BitmapFactory;
进口android.net.Uri;
进口android.os.Bundle;
进口android.os.Environment;
进口android.view.LayoutInflater;
进口android.view.View;
进口android.widget.ImageView;
进口android.widget.Toast;
进口android.provider.MediaStore;公共类MainActivity扩展ActionBarActivity { 私有静态最终诠释CAPTURE_IMAGE_ACTIVITY_REQUEST_ code = 100;@覆盖
公共无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.activity_main);
} 公共无效捕获(查看视图){ //创建意图拍照和控制返回给调用应用程序
意向意图=新意图(MediaStore.ACTION_IMAGE_CAPTURE);
文件imagesFolder =新的文件(Environment.getExternalStorageDirectory(),DCIM /相机);
imagesFolder.mkdirs();
文件图像=新的文件(imagesFolderimage_001.jpg);
乌里uriSavedImage = Uri.fromFile(图像); //创建一个文件保存图像
intent.putExtra(MediaStore.EXTRA_OUTPUT,uriSavedImage); //设置图像文件名 //启动图像拍摄意图
startActivityForResult(意向,CAPTURE_IMAGE_ACTIVITY_REQUEST_ code);
} 公共无效上传(查看视图){ 字符串路径= Environment.getExternalStorageDirectory()+/DCIM/Camera/image_001.jpg
文件imgFile =新的文件(路径);
如果(imgFile.exists())
{
位图MYBITMAP = BitmapFactory.de codeFILE(imgFile.getAbsolutePath());
ImageView的MYIMAGE =(ImageView的)findViewById(R.id.Display);
myImage.setImageBitmap(MYBITMAP);
}
其他
Toast.makeText(view.getContext(),没有形象是preSENT',Toast.LENGTH_SHORT).show();
}
}
从未被使用XML文件。
的的setContentView()
为您的活动包含 activity_main
。
检查您的布局 activity_main
- 里面设置一个按钮,运行code,因为它是,你会看到按钮
你想要的是设置 fragment_main
。
做到这一点的方法之一是:
公共类MainActivity扩展活动//或ActionBarActivity - 考虑它扩展了活动
{
@覆盖
保护无效的onCreate(捆绑savedInstanceState)
{
super.onCreate(savedInstanceState);
的setContentView(R.layout.activity_main);如果(savedInstanceState == NULL)
{
getFragmentManager()调用BeginTransaction()
。新增(R.id.container,新PlaceholderFragment())提交()。
}
}公共静态类PlaceholderFragment扩展片段
{公共PlaceholderFragment()
{
}@覆盖
公共查看onCreateView(LayoutInflater充气器,容器的ViewGroup,
捆绑savedInstanceState)
{
查看rootView = inflater.inflate(R.layout.fragment_main,集装箱,
FALSE); //这里是你的片段布局
返回rootView;
}
}
}
REF进一步:结果
1. 例如:样品的默认结果
2. 多窗格发展:片段结果
3. 如何使用碎片工作 - 安卓
I have been trying to display two buttons on an Android app. However, the buttons are not visible and the whole page is a blank. This is my XML code-
<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>
When I view the above code in the Graphical Layout, it shows the buttons perfectly. So, I guess that means it's not an issue of match_parent
or wrap_content
. I even tried to see if introducing a text view would make a difference. Again, it showed up on the Graphical Layout perfectly but on my device, only a blank screen is visible.
Could it be a problem of my device, instead of the code?
P.S. The device I am using is Motorola Moto G, which has a 4.5 inches display.
EDIT: the XML file is named fragment_main. Here's my JAVA code. package 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();
}
}
Your xml file is never being used.
The setContentView()
for your activity contains activity_main
.
Check your layout activity_main
- set a button inside and run the code as it is, you will see that button.
What you want is to set fragment_main
.
One way to do it is:
public class MainActivity extends Activity //or ActionBarActivity--considering it extends activity
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null)
{
getFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
}
public static class PlaceholderFragment extends Fragment
{
public PlaceholderFragment()
{
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
View rootView = inflater.inflate(R.layout.fragment_main, container,
false);//here is your fragment layout
return rootView;
}
}
}
Further ref:
1. Example: sample default
2. Multi pane development: fragments
3. How to work with fragments - android
这篇关于Android的按钮显示在图形布局,但不是在装置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!