底部屬性低於textView縮放錯誤,TextView在每個Android設備上的高度不同:請參閱附加圖片。TextView在不同分辨率下的高度不同。 DIP不起作用
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>
下面是手機的兩個例子,但它看起來不同的每部手機上...
Samsung Galax Ÿ標籤3精簡版(density1.0)
三星Galaxy S4(density3.0)
getDimension()應該正確縮放。我認爲你看到的問題來自高度和文本大小的硬編碼值。你應該一直使用getDimension(),getDimensionPixelSize()和getDimensionPixelOffset()。爲什麼你不用xml編碼這個任何特定的原因?更容易麻煩 - 拍攝這些問題。 – 323go
@ 323go今天我會試穿。有很多不同的動態屏幕,所以我選擇在運行時以編程方式構建視圖。儘管我有與XML相同的問題。 –
@ 323go我已將offset(getDimensionPixelOffset)和pxSize(getDimensionPixelSize)添加到代碼中,但這些值不能解決問題。對於1個DIP,它們返回50和3個DIP,它們都返回120.因此,它們只是底部* 3 –