无法在三星 s8 中扩展图像

用户3792429

由于三星 s8 的屏幕高度非常大。我在 LoginUI(附加)中的图像看起来很小。我以编程方式计算了手机的 dp 高度。

DisplayMetrics displayMetrics = getResources().getDisplayMetrics();
        float dpHeight = displayMetrics.heightPixels / displayMetrics.density;
        Log.d("checkheightdp",dpHeight+"");

它给了我 dpheight 692。所以,我创建了一个单独的 layout-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 = "[email protected]";
    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;

}

}

在此处输入图片说明

用户3792429

最后我得到了答案,为什么创建单独的高度布局不起作用。虽然三星 s8 的屏幕高度是 692 dp,但是 25dp 是不可用的,因为它被系统 UI 使用。显然它在 developer.android.com 中有说明。

Android 系统可能会使用部分屏幕作为系统 UI(例如屏幕底部的系统栏或顶部的状态栏),因此部分屏幕可能无法用于您的布局。因此,从 692 中减去 25 dp 得到 667dp。Creating layout-h667dp 有效。

我去了这个帖子。https://medium.com/@elye.project/an-important-note-when-managing-different-screen-height-3140e26e381a

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

三星Galaxy S8上的Unity / ARCore-相机图像已拉伸

三星s8和s9中的Android外部写入权限问题

在活动结果中,相机意图在三星s4中返回null

在三星Galaxy S5中将导出APK放在哪里

在三星 S9 中卸载应用程序后 SharedPreferences 不清晰

如何在三星S2中读取'/data/anr/traces.txt'

三星S8或相同版本7.0中未显示Google地图中的自定义标记

自 2.16.2 起关闭定位功能时,Altbeacon 在三星 S8 上找不到信标

三星Galaxy S8全屏模式

优化三星 S8 全屏应用

Media Recorder在三星Galaxy S3前置摄像头的Android中启动失败

三星S6中的音轨无法通过蓝牙工作

改造无法在数据上在三星S3安卓4.0.4版上发布

在三星智能电视中,无法将启动文件从index.html更改为其他文件吗?

三星Galaxy S8的Cordova应用触发游戏模式

三星S8牛轧糖始终显示(AOD)API

为三星S8应用正确的调光文件

从相机获取图像时,Android应用程序仅在三星设备中崩溃

从我的应用程序捕获的图像仅在三星设备中旋转

无法在三星 S20、S20+ 和 S20 Ultra 上生成用于生物识别身份验证的密钥对

如何在三星笔记本电脑上加快Windows 8?

Google ARcore在三星银河系Note 8上的表面检测

使用面部解锁的BiometricPrompt在三星S9上崩溃

在三星S4上打开GL错误的配置错误

MapView在三星S Duos上不显示地图

DragEventListener在三星S7(6.0)上不起作用

在三星Galaxy S2 Plus上移植Ubuntu Touch

Javascipt 在三星互联网浏览器中不起作用

应用程序意外停止,请在三星银河ace中重试