Questions tagged «android»

Android是Google的移动操作系统,用于对数字设备(智能手机,平板电脑,汽车,电视,Wear,Glass和IoT)进行编程或开发。对于与Android相关的主题,请使用特定于Android的标签,例如android-intent,android-activity,android-adapter等。对于与开发或编程无关但与Android框架相关的问题,请使用以下链接:https:// android.stackexchange.com。

2
Android-编写自定义(复合)组件
我当前正在开发的Android应用程序的主要活动已经变得非常大。这主要是因为它包含TabWidget带有3个标签的。每个选项卡都有很多组件。活动必须立即控制所有这些组件。因此,我想您可以想象这个Activity有20个字段(几乎每个组件都有一个字段)。它还包含很多逻辑(单击侦听器,填充列表的逻辑等)。 我通常在基于组件的框架中所做的就是将所有内容拆分为自定义组件。每个自定义组件将承担明确的责任。它包含它自己的一组组件以及与该组件相关的所有其他逻辑。 我试图弄清楚如何做到这一点,并且在Android文档中找到了他们喜欢称之为“复合控件”的东西。(请参阅http://developer.android.com/guide/topics/ui/custom-components.html#compound并滚动到“复合控件”部分)我想基于定义了视图结构。 在文档中说: 请注意,就像使用Activity一样,您可以使用声明式(基于XML)方法来创建所包含的组件,也可以从代码中以编程方式嵌套它们。 好吧,这是个好消息!基于XML的方法正是我想要的!但是它没有说如何去做,除了它“像一个Activity”一样。但是我在Activity中要做的是调用setContentView(...)以从XML扩展视图。如果您例如使用subclass,则该方法不可用LinearLayout。 因此,我尝试像这样手动将XML充气: public class MyCompoundComponent extends LinearLayout { public MyCompoundComponent(Context context, AttributeSet attributeSet) { super(context, attributeSet); LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); inflater.inflate(R.layout.my_layout, this); } } 除了我正在加载的XML已LinearLayout声明为根元素这一事实外,此方法都有效。这导致膨胀LinearLayout的孩子MyCompoundComponent本身已经是一个孩子LinearLayout!因此,现在我们之间有了一个多余的LinearLayout MyCompoundComponent及其实际需要的视图。 有人可以为我提供一种更好的方法来解决此问题,避免冗余LinearLayout实例化吗?

10
Android:清除活动堆栈
我的应用程序中有几个活动。而且流程非常复杂。当我单击注销应用程序时,导航到登录屏幕,然后用户可以通过取消按钮(调用system.exit(0))退出 当我退出或返回按钮时,系统从堆栈中调用一个活动:(当我进入“登录”屏幕时,如何清除堆栈中的所有活动?调用finish()是不实际的,因为有太多活动,并且某些活动不应在关闭时关闭它们是活动的,例如本地相机调用活动。 validateuser logoutuser = new validateuser(); logoutuser.logOut(); Intent loginscreen = new Intent(homepage.this, Login2.class); (homepage.this).finish(); loginscreen.setFlags( Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NO_HISTORY); startActivity(loginscreen);

8
Android TableLayout中的“ colspan”等效项是什么?
我在Android中使用TableLayout。现在,我有一个带有两个项目的TableRow,下面有一个带有它的TableRow。它呈现如下: ----------------------------- | Cell 1 | Cell 2 | ----------------------------- | Cell 3 | --------------- 我想要做的是使Cell 3延伸到两个上方的单元格,所以看起来像这样: ----------------------------- | Cell 1 | Cell 2 | ----------------------------- | Cell 3 | ----------------------------- 在HTML中,我将使用COLSPAN ....如何在Android中实现此功能?

8
确定JSON是JSONObject还是JSONArray
我将从服务器接收JSON对象或数组,但是我不知道它将是什么。我需要使用JSON,但是要这样做,我需要知道它是对象还是数组。 我正在使用Android。 有没有人有这样做的好方法?
132 android  arrays  json  object 

10
如何以编程方式一劳永逸地获取android屏幕尺寸?
如何以触摸事件和查看测量/布局所使用的单位以编程方式找出屏幕尺寸?换句话说,我想要屏幕右下角的坐标,在触摸事件' getRawX()/getRawY()和所使用的坐标系中 View.getLocationOnScreen()。 我不愿意将所需的事件/视图单位称为“像素”,因为我的手机在各种模式下显然都存在“像素”的几种概念,而且它们并没有形成一致的故事。 我认为这已被要求,并回答了很多计算器和其他地方,但没有答案我的手机上实际工作(DROID 4的Android 4.1.2)在所有模式: 我可以在Android应用中以编程方式找出屏幕宽度吗? 如何在Android中以编程方式获取ScreenSize Android根据屏幕大小以编程方式设置了“视图可见性” 如何获得Android屏幕的高度? 在Android中获取屏幕高度 https://groups.google.com/forum/#!topic/android-developers/IpxnfvDFrpc http://shuklaxyz.blogspot.com/2012/02/how-to-programmatically-figure-out.html http://coderock.net/how-to-determine-screen-resolution-programmatically/ http://www.codeproject.com/Tips/313848/Get-actual-screen-size-for-the-application-layout Android并以dp单位编程设置宽度和高度 http://www.simplecodestuffs.com/how-to-get-screen-dimensions-programmatically-in-android/ http://www.androidsnippets.com/get-size-and-orientation-of-the-screen http://grokbase.com/t/gg/android-developers/127aatfqb6/how-to-determine-screen-resolution-programmatically (哇!) 这适用于需要运行的库代码,而不管应用程序是否处于“屏幕兼容模式”(即清单中的targetSdkVersion <= 10),而且我也不希望对位置,大小做任何假设,或状态栏或任何其他类似的屏幕装饰的存在。 这是事实: 我的手机(运行android 4.1.2的droid 4)的物理像素为540x960,即彩色发光点很少。 如果应用程序处于屏幕兼容模式,则从观看触摸事件和查看测量值开始,屏幕尺寸的单位为360x640;如果应用程序处于屏幕兼容模式,则屏幕尺寸为540x960。这些是我需要以编程方式找到的数字,而不会为触摸事件或视图带来麻烦,但是我很难找到任何会返回这些数字的API。 以各种方式获得的Display和DisplayMetrics对象都声称屏幕尺寸为540 x 960“像素”(无论是否在屏幕兼容模式下)。具体而言,以下所有内容始终都是540x960:DisplayMetrics。{width,height}像素,Display.getSize(),Display.getRealSize(),Display.get {Width,Height}(), 以各种方式获得的配置对象都表示screen {Width,Height} Dp = 360x614(无论是否处于屏幕兼容模式)。我不认为这代表了整个屏幕,因为宽高比是错误的。(我认为这是整个屏幕减去状态栏;我需要整个屏幕。)我认为可以说整个屏幕为360x640 dp,尽管我不知道有返回该640的API。 通过各种方式获得的DisplayMetrics表示,在屏幕兼容模式下,“密度”为1.0f,在非屏幕兼容模式下为1.5f。 该活动 getWindow().getAttributes().{width,height} 没有帮助,因为它通常包含MATCH_PARENT 而非实际大小。但是我显然可以从活动的答案中得到所需的答案 getWindow().getDecorView().getMeasured{Width,Height}() (这实际上令人惊讶,因为活动窗口的decorView 看起来好像不占用整个屏幕;看起来好像是在减去状态栏占用了屏幕)。但是我不想依赖于此,因为如果窗口调整了大小(出现了软键盘?有人在调用window.setAttributes()?或者也许我根本不在活动中),这显然就是全部了。错误。 我了解以下公式应该成立:像素= dp *密度当未处于屏幕兼容模式时,这似乎与所有报告的数字(上述(3),(4),(5))一致:540x960 …
132 android 

7
单击通知后,Android通知不会消失
如果通知有问题,我想显示在通知栏中。尽管我将通知标记设置为Notification.DEFAULT_LIGHTS & Notification.FLAG_AUTO_CANCEL,单击通知后通知不会消失。有什么想法我做错了吗? NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); int icon = R.drawable.icon; CharSequence tickerText = "Ticker Text"; long time = System.currentTimeMillis(); Notification notification = new Notification(icon, tickerText, time); notification.flags = Notification.DEFAULT_LIGHTS & Notification.FLAG_AUTO_CANCEL; Context context = getApplicationContext(); CharSequence contentTitle = "Title"; CharSequence contentText = "Text"; Intent notificationIntent = new Intent(this, …

8
测量要在Canvas上绘制的文本高度(Android)
有没有直接测量文字高度的方法?我现在的操作方式是使用Paint measureText()来获取宽度,然后通过反复试验找到一个值来获取近似高度。我也一直在摸索FontMetrics,但是所有这些似乎都是很糟糕的近似方法。 我正在尝试按不同的分辨率缩放内容。我可以做到,但是最终我得到了冗长的冗长代码,需要大量计算才能确定相对大小。我讨厌它!一定有更好的方法。

11
Android Camera:数据意图返回null
我有一个包含多个活动的android应用程序。 在其中一个中,我正在使用一个按钮,它将称为设备摄像头: public void onClick(View view) { Intent photoIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); startActivityForResult(photoIntent, IMAGE_CAPTURE); } 在同一活动中,我调用OnActivityResult图像结果的方法: @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode == IMAGE_CAPTURE) { if (resultCode == RESULT_OK) { Bitmap image = (Bitmap) data.getExtras().get("data"); ImageView imageview = (ImageView) findViewById(R.id.pic); imageview.setImageBitmap(image); } else if …

10
带有新Firebase Cloud Messaging系统的通知图标
昨天,Google在Google I / O上展示了基于新Firebase的新通知系统。我使用Github上的示例尝试了这个新的FCM(Firebase Cloud Messaging)。 尽管我已声明特定的可绘制对象,但通知的图标始终是ic_launcher 为什么呢 下面是用于处理邮件的官方代码 public class AppFirebaseMessagingService extends FirebaseMessagingService { /** * Called when message is received. * * @param remoteMessage Object representing the message received from Firebase Cloud Messaging. */ // [START receive_message] @Override public void onMessageReceived(RemoteMessage remoteMessage) { // If the application is …

11
从矢量可绘制获取位图
在我的应用程序中,我必须为通知设置一个大图标。LargeIcon必须是位图,而我的可绘制对象是矢量图像(Android的新功能,请参阅此链接)问题是,当我尝试解码作为矢量图像的资源时,返回空值。 这是代码示例: if (BitmapFactory.decodeResource(arg0.getResources(), R.drawable.vector_menu_objectifs) == null) Log.d("ISNULL", "NULL"); else Log.d("ISNULL", "NOT NULL"); 在此示例中,当我用“正常”图像(例如png)替换R.drawable.vector_menu_objectifs时,结果不为null(我得到了正确的位图)是否缺少某些内容?

3
如何添加自定义按钮状态
例如,默认按钮在其状态和背景图片之间具有以下依赖关系: <?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_window_focused="false" android:state_enabled="true" android:drawable="@drawable/btn_default_normal" /> <item android:state_window_focused="false" android:state_enabled="false" android:drawable="@drawable/btn_default_normal_disable" /> <item android:state_pressed="true" android:drawable="@drawable/btn_default_pressed" /> <item android:state_focused="true" android:state_enabled="true" android:drawable="@drawable/btn_default_selected" /> <item android:state_enabled="true" android:drawable="@drawable/btn_default_normal" /> <item android:state_focused="true" android:drawable="@drawable/btn_default_normal_disable_focused" /> <item android:drawable="@drawable/btn_default_normal_disable" /> </selector> 如何定义自己的自定义状态(类似android:state_custom),然后可以使用它动态更改按钮的视觉外观?
132 android  button  state 

7
ConstraintLayout内部的Wrap_content视图延伸到屏幕外部
我正在尝试使用来实现一个简单的聊天气泡ConstraintLayout。这是我要实现的目标: 但是,wrap_content似乎不能在约束条件下正常工作。它尊重边距,但不能正确计算可用空间。这是我的布局: <?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout 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="wrap_content"> <TextView android:id="@+id/chat_message" android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="16dp" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" app:layout_constraintHorizontal_bias="0" tools:background="@drawable/chat_message_bubble" tools:text="Lorem ipsum dolor sit amet, consectetur adipiscing elit. Mauris sodales accumsan tortor at bibendum." android:layout_marginStart="64dp" android:layout_marginLeft="64dp" android:layout_marginEnd="32dp" android:layout_marginRight="32dp" android:layout_marginTop="8dp" android:layout_marginBottom="8dp" /> </android.support.constraint.ConstraintLayout> 如下所示: 我正在使用com.android.support.constraint:constraint-layout:1.0.0-beta4。 难道我做错了什么?它是错误还是只是非直觉的行为?ConstraintLayout我可以使用来实现正确的行为吗(我知道我可以使用其他布局,我正在ConstrainLayout专门询问)。

14
如何从SD卡删除文件?
我正在创建一个文件以作为电子邮件附件发送。现在,我想在发送电子邮件后删除图像。有没有办法删除文件? 我已经尝试过,myFile.delete();但是没有删除文件。 我正在将此代码用于Android,因此编程语言是Java,使用通常的Android方法访问SD卡。发送电子邮件后,将an返回到屏幕onActivityResult时,我正在方法中删除文件Intent。

13
Android使用键盘上的“完成”按钮单击按钮
好的,在我的应用程序中,我有一个供用户输入数字的字段。我将字段设置为仅接受数字。当用户单击该字段时,它会弹出键盘。在键盘上(在ICS上)有一个完成按钮。我希望键盘上的完成按钮触发我的应用程序中的提交按钮。我的代码如下。 package com.michaelpeerman.probability; import android.app.Activity; import android.app.ProgressDialog; import android.content.DialogInterface; import android.content.DialogInterface.OnCancelListener; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; import java.util.Random; public class ProbabilityActivity extends Activity implements OnClickListener { private Button submit; ProgressDialog dialog; int increment; Thread background; int heads = 0; …

13
如何呼叫Android通讯录?
我正在制作一个Android应用程序,需要拨打电话的联系人列表。我需要调用联系人列表功能,选择一个联系人,然后使用联系人的名字返回我的应用程序。这是我在互联网上获得的代码,但是没有用。 import android.app.ListActivity; import android.content.Intent; import android.database.Cursor; import android.os.Bundle; import android.provider.Contacts.People; import android.view.View; import android.widget.ListAdapter; import android.widget.ListView; import android.widget.SimpleCursorAdapter; import android.widget.TextView; public class Contacts extends ListActivity { private ListAdapter mAdapter; public TextView pbContact; public static String PBCONTACT; public static final int ACTIVITY_EDIT=1; private static final int ACTIVITY_CREATE=0; // Called when …
131 android  contacts 

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.