不同设备或模拟器上的 EditText 处于不同的位置
EditText on different devices or emulator is in a deferent position
我将下面的代码用于其中带有 EditText
的弹出警告框。我正在使用边距来对齐下面代码中的 EditText
。问题是我用于一个设备或模拟器的边距数字将在另一台设备或模拟器上关闭。如果我正确对齐一个设备,它将在另一台设备上看起来不正确。任何帮助表示赞赏。
AlertDialog.Builder aBuilder = new AlertDialog.Builder(ExpenseSheet.this);
LinearLayout layout = new LinearLayout(this);
layout.setOrientation(LinearLayout.VERTICAL);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
params.setMargins(85, 0, 50, 0);
// Set up the input
final EditText input = new EditText(ExpenseSheet.this);
//FOCUSING ON POPUP WINDOW TEXT
input.requestFocus();
layout.addView(input, params);
// Specify the type of input expected; this, for example, sets the input as a password, and will mask the text
input.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);
//limiting the amount of characters
input.setFilters(new InputFilter[]{new InputFilter.LengthFilter(8)});
aBuilder.setView(layout);
问题是您使用的是默认代表像素的边距的硬编码数字。根据设备的屏幕密度,此像素数可以转换为更长或更短的实际长度。
您应该尝试将这些值乘以屏幕密度,以使它们在各种屏幕密度的设备上看起来保持不变。
示例:
final DisplayMetrics metrics = context.getResources().getDisplayMetrics();
final float screenDensity = metrics.density;
double mWidth = 25 * screenDensity;// constant width across densities
double mHeight = 30 * screenDensity;// constant height across densities
现在您可以使用此类变量而不是硬编码值来获得跨各种屏幕密度的统一效果。
我将下面的代码用于其中带有 EditText
的弹出警告框。我正在使用边距来对齐下面代码中的 EditText
。问题是我用于一个设备或模拟器的边距数字将在另一台设备或模拟器上关闭。如果我正确对齐一个设备,它将在另一台设备上看起来不正确。任何帮助表示赞赏。
AlertDialog.Builder aBuilder = new AlertDialog.Builder(ExpenseSheet.this);
LinearLayout layout = new LinearLayout(this);
layout.setOrientation(LinearLayout.VERTICAL);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
params.setMargins(85, 0, 50, 0);
// Set up the input
final EditText input = new EditText(ExpenseSheet.this);
//FOCUSING ON POPUP WINDOW TEXT
input.requestFocus();
layout.addView(input, params);
// Specify the type of input expected; this, for example, sets the input as a password, and will mask the text
input.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);
//limiting the amount of characters
input.setFilters(new InputFilter[]{new InputFilter.LengthFilter(8)});
aBuilder.setView(layout);
问题是您使用的是默认代表像素的边距的硬编码数字。根据设备的屏幕密度,此像素数可以转换为更长或更短的实际长度。
您应该尝试将这些值乘以屏幕密度,以使它们在各种屏幕密度的设备上看起来保持不变。
示例:
final DisplayMetrics metrics = context.getResources().getDisplayMetrics();
final float screenDensity = metrics.density;
double mWidth = 25 * screenDensity;// constant width across densities
double mHeight = 30 * screenDensity;// constant height across densities
现在您可以使用此类变量而不是硬编码值来获得跨各种屏幕密度的统一效果。