由于三星s8的屏幕高度非常大。我在LoginUI(附件)中的图像看起来很小。我以编程方式计算了手机在dp中的高度。
DisplayMetrics displayMetrics = getResources().getDisplayMetrics();
float dpHeight = displayMetrics.heightPixels / displayMetrics.density;
Log.d("checkheightdp",dpHeight+"");
它给了我dpheight 692.So,我创建了一个单独的布局-h692dp,并使我的布局增加了图像高度。由于某种原因,它仍然选择默认布局,因此图像不会扩展。 我的代码: 公共类LoginActivity扩展了AppCompatActivity {
private EditText email, psd;
public ImageView deleteEmail;
public ImageView deletePsd;
public ImageView contactUs;
GlobalProvider globalProvider;
private int a = 0;
private Button sign_in_button, tourist_in_button;
private List<String> history = new ArrayList<String>();
private String usernameStr, newVersion = "x";
public static String character = "character";
private ConnectivityManager mConnectivityManager;
private NetworkInfo netInfo;
private ProgressDialog dialog;
public Thread thread;
private BroadcastReceiver myNetReceiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (action.equals(ConnectivityManager.CONNECTIVITY_ACTION)) {
mConnectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
netInfo = mConnectivityManager.getActiveNetworkInfo();
if (netInfo == null) {
Toast.makeText(LoginActivity.this, "网络链接不可用!", Toast.LENGTH_SHORT).show();
}
}
}
};
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
globalProvider = GlobalProvider.getInstance(LoginActivity.this);
IntentFilter mFilter = new IntentFilter();
mFilter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
registerReceiver(myNetReceiver, mFilter);
//找到对象email、psd、sign_in_button并绑定监听事件
email = (EditText) findViewById(R.id.email);
psd = (EditText) findViewById(R.id.psd);
contactUs = (ImageView) findViewById(R.id.contact_us);
sign_in_button = (Button) findViewById(R.id.sign_in_button);
tourist_in_button = (Button) findViewById(R.id.tourist_in_button);
deleteEmail = (ImageView) findViewById(R.id.deleteEmail);
deletePsd = (ImageView) findViewById(R.id.deletePsd);
// gview=(GridView) findViewById(R.id.grid_layout);
// ImageAdapter imgAdapter=new ImageAdapter(LoginActivity.this);
//gview.setAdapter(imgAdapter);
contactUs.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(LoginActivity.this, ContactActivity.class);
startActivity(intent);
}
});
deleteEmail.setVisibility(View.GONE);
deletePsd.setVisibility(View.GONE);
try {
findHistoryList();
} catch (IOException e) {
e.printStackTrace();
}
email.setOnFocusChangeListener(new View.OnFocusChangeListener() {
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
deleteEmail.setVisibility(View.VISIBLE);
} else {
deleteEmail.setVisibility(View.GONE);
}
}
});
psd.setOnFocusChangeListener(new View.OnFocusChangeListener() {
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
deletePsd.setVisibility(View.VISIBLE);
} else {
deletePsd.setVisibility(View.GONE);
}
}
});
deleteEmail.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
email.setText("");
}
});
deletePsd.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
psd.setText("");
}
});
sign_in_button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (email.getText() == null || email.getText().toString().equals("") || psd.getText() == null || email.getText().toString().equals("")) {
new AlertDialog.Builder(LoginActivity.this)
.setMessage(getString(R.string.notempty))
.setPositiveButton(getString(R.string.confirm), new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
}
}).show();
} else {
try {
setHistoryList();
} catch (IOException e) {
e.printStackTrace();
}
dialog = new ProgressDialog(LoginActivity.this);
dialog.setMessage(getString(R.string.loging));
dialog.show();
sign_in_button.setEnabled(false);
loginAction(v);
}
}
});
//为sign_in_button绑定监听事件,调用loginAction(v)
tourist_in_button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
dialog = new ProgressDialog(LoginActivity.this);
dialog.setMessage(getString(R.string.loging));
dialog.show();
sign_in_button.setEnabled(false);
loginActionTourist(v);
}
});
}
public static void setCharacter(Context context, String cha) {
SharedPreferences settings = PreferenceManager
.getDefaultSharedPreferences(context);
SharedPreferences.Editor editor = settings.edit();
editor.putString(character, cha);
editor.commit();
}
public static String getToken(Context context) {
SharedPreferences settings = PreferenceManager
.getDefaultSharedPreferences(context);
String cha = settings.getString(character, ""/*default value is ""*/);
//Log.v("err", tokenStr);
return cha;
}
//创建loginAction()方法
public void loginAction(View view) {
//分别把email、psd的值传递给usernameStr、passwordStr
final String usernameStr = email.getText().toString();
String passwordStr = psd.getText().toString();
Log.d("checkentries",usernameStr+" "+passwordStr);
setCharacter(this, "user");
// Log.d("chkpassword",passwordStr);
//GlobalProvider.getInstance().character =
Map<String, String> params = new HashMap<>();
params.put("password", passwordStr);
params.put("email",usernameStr );
CustomRequest jsonObjectRequest = new CustomRequest(Request.Method.POST, loginUrlStr, params, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
Log.d("responsevolley", response.getString("token"));
dialog.dismiss();
globalProvider.IsLoging = true;
sign_in_button.setEnabled(true);
String token;
token = response.getString("token");
token = token.replaceAll("\"", "");//把token中的"\""全部替换成""
Constants.setToken(LoginActivity.this, token);
globalProvider.isLogined=true;
Intent intent = new Intent(LoginActivity.this, MainActivity.class);
intent.putExtra("isLogin", usernameStr);
startActivity(intent);
//this.setResult(Activity.RESULT_OK);//为结果绑定Activity.RESULT_OK
finish();
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.d("errorvolley", error.toString());
dialog.dismiss();
new AlertDialog.Builder(LoginActivity.this)
.setMessage(getString(R.string.errorId))
.setPositiveButton(getString(R.string.confirm), new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
}
}).show();
sign_in_button.setEnabled(true);
}
});
globalProvider.addRequest(jsonObjectRequest);
}
public void loginActionTourist(View view) {
usernameStr = "guest@veg.com";
String passwordStr = "12345678";
setCharacter(this, "tourist");
globalProvider.isLogined = true;
// GlobalProvider.getInstance().character = "tourist";
// 绑定参数
Map<String,String> params = new HashMap();
params.put("email", usernameStr);
params.put("password", passwordStr);
CustomRequest customRequest=new CustomRequest(Request.Method.POST, loginUrlStr, params, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
dialog.dismiss();
globalProvider.IsLoging = true;
tourist_in_button.setEnabled(true);
String token;
try {
token = response.getString("token");
token = token.replaceAll("\"", "");
Constants.setToken(LoginActivity.this, token);
} catch (JSONException e) {
e.printStackTrace();
}
//把token中的"\""全部替换成""
Intent intent = new Intent(LoginActivity.this, MainActivity.class);
intent.putExtra("isLogin", usernameStr);
startActivity(intent);
//this.setResult(Activity.RESULT_OK);//为结果绑定Activity.RESULT_OK
finish();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
dialog.dismiss();
new AlertDialog.Builder(LoginActivity.this)
.setMessage(getString(R.string.errorId))
.setPositiveButton(getString(R.string.confirm), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
}).show();
tourist_in_button.setEnabled(true);
}
});
}
private void findHistoryList() throws IOException {
File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/EasybuyCustomer.txt");
FileInputStream fis = new FileInputStream(file);
InputStreamReader isr = new InputStreamReader(fis, "UTF-8");
BufferedReader br = new BufferedReader(isr);
String s = null;
while ((s = br.readLine()) != null) {
history.add(s);
//System.out.println(arrs[0] + " : " + arrs[1] + " : " + arrs[2]);
}
if (history.size() > 0) {
email.setText(history.get(0));
psd.setText(history.get(1));
}
br.close();
isr.close();
fis.close();
}
public void setHistoryList() throws IOException {
FileOutputStream fos = new FileOutputStream(Environment.getExternalStorageDirectory().getAbsolutePath() + "/EasybuyCustomer.txt");
OutputStreamWriter osw = new OutputStreamWriter(fos, "UTF-8");
BufferedWriter bw = new BufferedWriter(osw);
bw.write("");
bw.write(email.getText().toString());
bw.newLine();
bw.write(psd.getText().toString());
bw.newLine();
bw.close();
osw.close();
fos.close();
}
@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
if (ev.getAction() == MotionEvent.ACTION_DOWN) {
View v = getCurrentFocus();
if (isShouldHideInput(v, ev)) {
hideSoftInput(v.getWindowToken());
}
}
return super.dispatchTouchEvent(ev);
}
private boolean isShouldHideInput(View v, MotionEvent event) {
if (v != null && (v instanceof EditText)) {
int[] l = { 0, 0 };
v.getLocationInWindow(l);
int left = l[0], top = l[1], bottom = top + v.getHeight(), right = left
+ v.getWidth();
if (event.getX() > left && event.getX() < right
&& event.getY() > top && event.getY() < bottom) {
return false;
} else {
return true;
}
}
return false;
}
private void hideSoftInput(IBinder token) {
if (token != null) {
InputMethodManager im = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
im.hideSoftInputFromWindow(token,
InputMethodManager.HIDE_NOT_ALWAYS);
}
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
//按下键盘上返回按钮
if(keyCode == KeyEvent.KEYCODE_BACK){
new AlertDialog.Builder(this)
.setMessage(getString(R.string.youconfirmtologout))
.setNegativeButton(getString(R.string.cancel), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
})
.setPositiveButton(getString(R.string.confirm), new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
a=a+1;
finish();
}
}).show();
return true;
}else{
return super.onKeyDown(keyCode, event);
}
}
@Override
protected void onDestroy() {
super.onDestroy();
this.unregisterReceiver(myNetReceiver);
if(a>0){
System.exit(0);
}
a=0;
}
}
答案 0 :(得分:0)
最后我得到了答案,为什么创建单独的高度布局不起作用。 虽然三星s8的屏幕高度是692 dp,但25dp不可用,因为它被系统UI使用。 显然,它是在developer.android.com中说明的。
Android系统可能会将某些屏幕用于系统UI(例如屏幕底部的系统栏或顶部的状态栏),因此某些屏幕可能无法用于您的布局。 因此,从692减去25 dp得到667dp.Creating layout-h667dp工作。