TextView 在不同的分辨率下处于不同的高度。拨码不起作用
TextView is on different heights on different resolutions. DIP doesn't work
下方textView的bottom属性缩放错误,TextView在每个[=41上的高度不同=] 设备:见附图。
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.bars_layout);
RelativeLayout relativeLayout = (RelativeLayout)findViewById(R.id.bar_holder);
BarView view = new BarView(getApplicationContext());
int width = (int) getApplicationContext().getResources().getDimension(R.dimen.bar_width_compare);
int height = 200;
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(width, height);
params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);
int left = (int) getApplicationContext().getResources().getDimension(R.dimen.bar_margin_left_right);
int right = 0;
int bottom = (int) getApplicationContext().getResources().getDimension(R.dimen.graph_margin_bar_compare_bottom);
params.setMargins(left, 0, right, bottom);
view.setBackgroundColor(getApplicationContext().getResources().getColor(R.color.bar_dark_blue));
view.setLayoutParams(params);
relativeLayout.addView(view);
TextView textView = new TextView(getApplicationContext());
textView.setText(" 20 ");
textView.setTextSize(20);
textView.setTextColor(getApplicationContext().getResources().getColor(R.color.black_text));
width = (int) getApplicationContext().getResources().getDimension(R.dimen.bar_width);
height = 100;
bottom = (int) getApplicationContext().getResources().getDimension(R.dimen.graph_margin_bar_bottom);
int offset = getApplicationContext().getResources().getDimensionPixelOffset(R.dimen.graph_margin_bar_bottom);
int pxSize = getApplicationContext().getResources().getDimensionPixelSize(R.dimen.graph_margin_bar_bottom);
RelativeLayout.LayoutParams params2 = new RelativeLayout.LayoutParams(width, height);
params2.setMargins(0, 0, 0, bottom);
params2.addRule(Gravity.CENTER_HORIZONTAL);
params2.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
params2.addRule(RelativeLayout.ALIGN_LEFT, view.getId());
textView.setLayoutParams(params2);
relativeLayout.addView(textView);
}
}
bars_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="horizontal"
android:layout_marginTop="40dp" >
<RelativeLayout
android:id="@+id/bar_holder"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginTop="20dp"
android:layout_marginBottom="10dp" >
</RelativeLayout>
</LinearLayout>
dimens.xml
<resources>
<dimen name="graph_margin_bar_bottom">40dp</dimen>
<dimen name="bar_width_compare">25dp</dimen>
<dimen name="bar_margin_left_right">10dp</dimen>
<dimen name="graph_margin_bar_compare_bottom">50dp</dimen>
<dimen name="bar_width">50dp</dimen>
</resources>
这里有 2 个 phone 的例子,但每个 phone...
看起来都不一样
三星 Galaxy Tab 3 Lite(密度 1.0)
三星 Galaxy S4 (density3.0)
Android 将所有实际屏幕尺寸分为四种通用尺寸:小号、正常、大号和特大号。
为 "layout" 创建三个 floder 用于正常的小屏幕
layout-large 适用于大屏幕
layout-xlarge 用于超大屏幕
将相同的 bars_layout.xml 添加到所有 floder 并根据屏幕尺寸增加尺寸(例如,如果您在布局 floder 中保持尺寸 10 dp,那么对于 layout-large 将该尺寸增加到 30 dp,对于 layout-xlarge制作 60dp)
对于dimens.xml小而普通的屏幕,将其添加到"values" floder
对于大屏幕添加到 values-large floder
对于超大添加到 values-xlarge
setMargins(int,int,int,int) 使用的是像素,而不是 DIP,因此在每个相同尺寸的设备上(以像素为单位)看起来不同,您需要将 DIP 转换为像素,然后使用该值进行 setMargins()
将文本高度乘以 densityDpi 并将文本大小乘以 scaledDensity 值。所以它会支持不同的分辨率。
DisplayMetrics dm = context.getResources().getDisplayMetrics();
int densityDpi = dm.densityDpi;
float spi = dm.scaledDensity;
textheight = 100 * densityDpi;
textSize = 20 * spi;
下方textView的bottom属性缩放错误,TextView在每个[=41上的高度不同=] 设备:见附图。
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.bars_layout);
RelativeLayout relativeLayout = (RelativeLayout)findViewById(R.id.bar_holder);
BarView view = new BarView(getApplicationContext());
int width = (int) getApplicationContext().getResources().getDimension(R.dimen.bar_width_compare);
int height = 200;
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(width, height);
params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);
int left = (int) getApplicationContext().getResources().getDimension(R.dimen.bar_margin_left_right);
int right = 0;
int bottom = (int) getApplicationContext().getResources().getDimension(R.dimen.graph_margin_bar_compare_bottom);
params.setMargins(left, 0, right, bottom);
view.setBackgroundColor(getApplicationContext().getResources().getColor(R.color.bar_dark_blue));
view.setLayoutParams(params);
relativeLayout.addView(view);
TextView textView = new TextView(getApplicationContext());
textView.setText(" 20 ");
textView.setTextSize(20);
textView.setTextColor(getApplicationContext().getResources().getColor(R.color.black_text));
width = (int) getApplicationContext().getResources().getDimension(R.dimen.bar_width);
height = 100;
bottom = (int) getApplicationContext().getResources().getDimension(R.dimen.graph_margin_bar_bottom);
int offset = getApplicationContext().getResources().getDimensionPixelOffset(R.dimen.graph_margin_bar_bottom);
int pxSize = getApplicationContext().getResources().getDimensionPixelSize(R.dimen.graph_margin_bar_bottom);
RelativeLayout.LayoutParams params2 = new RelativeLayout.LayoutParams(width, height);
params2.setMargins(0, 0, 0, bottom);
params2.addRule(Gravity.CENTER_HORIZONTAL);
params2.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
params2.addRule(RelativeLayout.ALIGN_LEFT, view.getId());
textView.setLayoutParams(params2);
relativeLayout.addView(textView);
}
}
bars_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="horizontal"
android:layout_marginTop="40dp" >
<RelativeLayout
android:id="@+id/bar_holder"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginTop="20dp"
android:layout_marginBottom="10dp" >
</RelativeLayout>
</LinearLayout>
dimens.xml
<resources>
<dimen name="graph_margin_bar_bottom">40dp</dimen>
<dimen name="bar_width_compare">25dp</dimen>
<dimen name="bar_margin_left_right">10dp</dimen>
<dimen name="graph_margin_bar_compare_bottom">50dp</dimen>
<dimen name="bar_width">50dp</dimen>
</resources>
这里有 2 个 phone 的例子,但每个 phone...
看起来都不一样三星 Galaxy Tab 3 Lite(密度 1.0)
三星 Galaxy S4 (density3.0)
Android 将所有实际屏幕尺寸分为四种通用尺寸:小号、正常、大号和特大号。
为 "layout" 创建三个 floder 用于正常的小屏幕 layout-large 适用于大屏幕 layout-xlarge 用于超大屏幕
将相同的 bars_layout.xml 添加到所有 floder 并根据屏幕尺寸增加尺寸(例如,如果您在布局 floder 中保持尺寸 10 dp,那么对于 layout-large 将该尺寸增加到 30 dp,对于 layout-xlarge制作 60dp)
对于dimens.xml小而普通的屏幕,将其添加到"values" floder 对于大屏幕添加到 values-large floder 对于超大添加到 values-xlarge
setMargins(int,int,int,int) 使用的是像素,而不是 DIP,因此在每个相同尺寸的设备上(以像素为单位)看起来不同,您需要将 DIP 转换为像素,然后使用该值进行 setMargins()
将文本高度乘以 densityDpi 并将文本大小乘以 scaledDensity 值。所以它会支持不同的分辨率。
DisplayMetrics dm = context.getResources().getDisplayMetrics();
int densityDpi = dm.densityDpi;
float spi = dm.scaledDensity;
textheight = 100 * densityDpi;
textSize = 20 * spi;