我已经设置了一个简单的ViewPager,每页都有一个高度为200dp的ImageView.
这是我的寻呼机:
pager = new ViewPager(this);
pager.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
pager.setBackgroundColor(Color.WHITE);
pager.setOnPageChangeListener(listener);
layout.addView(pager);
Run Code Online (Sandbox Code Playgroud)
尽管将高度设置为wrap_content,但即使imageview仅为200dp,寻呼机也会填充屏幕.我试图用"200"替换寻呼机的高度,但这给了我多种分辨率的不同结果.我无法将"dp"添加到该值.如何在寻呼机的布局中添加200dp?
我正在尝试创建一个应用程序,当一个电话来到手机我想要检测数字.以下是我尝试过的,但它没有检测来电.
我想MainActivity在后台运行,我该怎么做?
我已经在manifest档案中给予了许可.
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
Run Code Online (Sandbox Code Playgroud)
我应该在清单中提供其他任何东西吗?
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test_layout);
}
public class myPhoneStateChangeListener extends PhoneStateListener {
@Override
public void onCallStateChanged(int state, String incomingNumber) {
super.onCallStateChanged(state, incomingNumber);
if (state == TelephonyManager.CALL_STATE_RINGING) {
String phoneNumber = incomingNumber;
}
}
}
}
Run Code Online (Sandbox Code Playgroud) 我已经编写了以下代码来添加Android手机本中的新联系人,它正在工作但是当我打开联系人菜单时,我看不到添加的新联系人.任何人都可以帮我找出这里有什么问题吗?
import android.app.Activity;
import android.os.Bundle;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.provider.ContactsContract;
import android.widget.TextView;
import android.widget.Toast;
public class AddContacts extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
try {
ContentResolver cr = this.getContentResolver();
ContentValues cv = new ContentValues();
cv.put(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME, "New Name");
cv.put(ContactsContract.CommonDataKinds.Phone.NUMBER, "1234567890");
cv.put(ContactsContract.CommonDataKinds.Phone.TYPE, ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE);
cr.insert(ContactsContract.RawContacts.CONTENT_URI, cv);
Toast.makeText(this, "Contact added", Toast.LENGTH_LONG).show();
} catch(Exception e) {
TextView tv = new TextView(this);
tv.setText(e.toString());
setContentView(tv);
}
}
}
Run Code Online (Sandbox Code Playgroud) 我对慢速网络上的Volley POST请求有问题.每次我BasicNetwork.logSlowRequests在LogCat中看到,我的POST请求都会执行两次或更多次,从而产生多个(2次或更多次)1个请求的帖子.我已经将重试策略设置为0,但它没有帮助.
这是我的LogCat
03-16 01:31:35.674:D/Volley(5984):[19807] BasicNetwork.logSlowRequests:请求的HTTP响应= <[] http:// [myserver]/api/places 0xfa7d0c33 NORMAL 1> [lifetime = 3824 ],[size = 313],[rc = 200],[retryCount = 0] 03-16 01:31:35.704:D/Volley(5984):[1] Request.finish:3853 ms:[] http://[myserver]/api/places 0xfa7d0c33 NORMAL 1
这是我的代码
JSONObject body = new JSONObject();
try {
body.put(PROTO_BODY_AUTHORIZATION, Sessions.getActiveSession().getToken());
} catch (JSONException e) {
e.printStackTrace();
}
JsonObjectRequest request = new JsonObjectRequest(
Request.Method.POST,
context.getResources().getString(R.string.server_address) + "/places",
body,
callback,
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(context, error.getMessage(), Toast.LENGTH_LONG).show();
}
}
);
request.setRetryPolicy(
new DefaultRetryPolicy(
DefaultRetryPolicy.DEFAULT_TIMEOUT_MS,
0, …Run Code Online (Sandbox Code Playgroud) 我正在尝试实现导航抽屉,但我不断收到此错误.我看到了类似的问题,但对我没有用.我有以下布局activity_main2.xml:
<android.support.v4.widget.DrawerLayout
android:layout_width="match_parent"
android:id="@+id/drawerLayout"
android:layout_height="match_parent">
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
<RelativeLayout
android:layout_gravity="left|start"
android:layout_width="match_parent"
android:background="#fff"
android:layout_height="match_parent">
<ListView
android:id="@+id/left_drawer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:divider="#eee"
android:background="#fff"
android:dividerHeight="1dp" />
</RelativeLayout>
</android.support.v4.widget.DrawerLayout>
Run Code Online (Sandbox Code Playgroud)
在我的activity_main2.java上
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
initView();
if (toolbar != null) {
//toolbar.setTitle("");
setSupportActionBar(toolbar);
}
initDrawer();
}
private void initView() {
leftDrawerList = (ListView) findViewById(R.id.left_drawer);
toolbar = (Toolbar) findViewById(R.id.toolbar);
drawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout);
navigationDrawerAdapter = new ArrayAdapter<String>( MainActivity.this, android.R.layout.simple_list_item_1, leftSliderData);
leftDrawerList.setAdapter(navigationDrawerAdapter);
leftDrawerList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long …Run Code Online (Sandbox Code Playgroud) 我需要执行一个Volley请求并等待响应解析它并返回它,但不知道如何实现这一点.有人可以帮忙吗?
我现在拥有的是:
public String getLink() {
JsonObjectRequest jsObjRequest = new JsonObjectRequest(Request.Method.GET, url, null, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
shortenURL = response.getString("url");
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
// TODO Auto-generated method stub
}
});
queue.add(jsObjRequest);
return shortenURL;
}
Run Code Online (Sandbox Code Playgroud) 介绍:
随着Android 6.0.1的新版本,似乎Android对Spinner组件进行了一些更改,因为默认情况下,胡萝卜周围的内部填充更大一些.
我在一个应用程序中注意到了这一点,我没有修改代码中的任何内容,只是更新了设备上的操作系统,但是微调器的大小不同.
情况:
我有两个旋转器,一个在另一个旁边RelativeLayout(介意其余组件,我添加了所有内容,所以你可以看到这部分布局 - 删除了完全不必要的属性或视图ID)
<RelativeLayout
android:id="@+id/header"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:id="@+id/container_for_buttons_on_the_right"
android:layout_width="wrap_content"
android:layout_height="48dp"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true">
<!-- Buttons here-->
</LinearLayout>
<android.support.v7.widget.AppCompatSpinner
android:id="@+id/spinner_1"
android:layout_width="wrap_content"
android:layout_height="48dp"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<ViewSwitcher
android:id="@+id/spinner_switch"
android:layout_width="wrap_content"
android:layout_height="48dp"
android:layout_toEndOf="@id/spinner_1"
android:layout_toLeftOf="@id/container_for_buttons_on_the_right"
android:layout_toRightOf="@id/spinner_1"
android:layout_toStartOf="@id/container_for_buttons_on_the_right"
android:inAnimation="@anim/fade_in"
android:outAnimation="@anim/fade_out">
<android.support.v7.widget.AppCompatSpinner
android:layout_width="wrap_content"
android:layout_height="match_parent" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<!-- ImageView properties are incomplete but I need it there.-->
</ViewSwitcher>
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)
Spinner适配器用于该getView()方法的布局如下:
<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="48dp"
android:orientation="horizontal"
android:paddingLeft="8dp"
android:paddingRight="8dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:ellipsize="end"
android:gravity="center_vertical"
android:singleLine="true"
tools:text="Test" …Run Code Online (Sandbox Code Playgroud) android android-layout android-spinner android-6.0-marshmallow android-6.0.1-marshmallow
代码大部分时间都有效,但有时会引发异常.无法弄清楚是什么原因造成的.
什么是在...创建文件
/storage/emulated/0/Download/theFileName.jpg
并向其写入数据(来自sourceFile,确实存在),但新创建的文件得到"文件不存在"异常.
(它确实有uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE", and uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"清单").
File sourceFile = new File(theSourceFileFullPath);
if (sourceFile.exists()) {
File downloadDirectory = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
String downloadPath = downloadDirectory.getPath();
String newFilePath = (downloadPath + "/" + fileName);
File newFile = new File(newFilePath);
try {
FileInputStream in = new FileInputStream(sourceFile);
// ava.io.FileNotFoundException:
// /storage/emulated/0/Download/theFileName.jpg: open failed: ENOENT (No such file or directory)
// exception at this line
FileOutputStream out = new FileOutputStream(newFile);
//......
} catch (Exception e) {}
}
Run Code Online (Sandbox Code Playgroud) 我的apiPath是完全动态的.我正在使用包含"ipAddress"和"SSLprotocol"等字段的项目.基于它们我可以建立我的网址:
private String urlBuilder(Server server) {
String protocol;
String address = "";
if (AppTools.isDeviceOnWifi(activity)) {
address = serverToConnect.getExternalIp();
} else if (AppTools.isDeviceOnGSM(activity)) {
address = serverToConnect.getInternalIp();
}
if (server.isShouldUseSSL()) {
protocol = "https://";
} else {
protocol = "http://";
}
return protocol + address;
}
Run Code Online (Sandbox Code Playgroud)
所以我的协议+地址可以是:http:// + 192.168.0.01:8010 = http://192.168.0.01:8010
我想这样使用它:
@FormUrlEncoded
@POST("{fullyGeneratedPath}/json/token.php")
Observable<AuthenticationResponse> authenticateUser(
@Path("fullyGeneratedPath") String fullyGeneratedPath,
@Field("login") String login,
@Field("psw") String password,
@Field("mobile") String mobile);
Run Code Online (Sandbox Code Playgroud)
因此,authenticateUser的完整路径将是http://192.168.0.01:8010/json/token.php - 例如.
这意味着我不需要任何basePath,因为我自己创建整个basePath,具体取决于我想要连接的服务器.
我的改造设置是:
@Provides
@Singleton
Retrofit provideRetrofit(OkHttpClient okHttpClient,
Converter.Factory converterFactory, …Run Code Online (Sandbox Code Playgroud) 我有一个活动进入另一个全屏幕活动.但是,当从此活动转换到我的全屏活动时,导航栏会向下滑动而不是立即消失.我在第二个活动中为一个全屏幕窗口充气,但是由于慢速滑动动画,它在动画完成后1秒后调整大小而不是立即充气到全屏幕.因此,我需要动画立即消失.我试过了
<item name="android:windowAnimationStyle">@null</item>
和
overridePendingTransition(0, 0);
Run Code Online (Sandbox Code Playgroud)
和
Transition fade = new Fade();
fade.excludeTarget(android.R.id.navigationBarBackground, true);
getWindow().setEnterTransition(fade);
Run Code Online (Sandbox Code Playgroud)
没有运气.
在Windows方面,我试过了
WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS
WindowManager.LayoutParams.FLAG_FULLSCREEN
WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN
Run Code Online (Sandbox Code Playgroud)
我如何隐藏导航栏: View.SYSTEM_UI_FLAG_HIDE_NAVIGATION