2017-03-18 93 views
1

我需要編輯的ImageView的絕對值:Android Studio中的約束佈局的變化絕對

tools:layout_editor_absoluteY="0dp" 

tools:layout_editor_absoluteY="50dp" 

但在Java中,如果我只是做

ImageView img = (ImageView)findViewById(R.id.mainView); 
ConstraintLayout.LayoutParams params1 = (ConstraintLayout.LayoutParams)(img.getLayoutParams()); 
params1.editorAbsoluteY = 50; 
img.setLayoutParams(params1); 

它不」工作。

回答

1

從DP轉換你的價值爲像素。

0

LayoutParams總是處理像素值。

因此,在設置LayoutParams上的值之前,您需要將DP轉換爲PX值。有很多這樣的例子,一個快速谷歌應該這樣做。

如果這不起作用,您需要提供更多信息,而不是簡單地說「它不起作用」。設置params1.editorAbsoluteY前

public int dpToPx(int dp) { 
    DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics(); 
    return Math.round(dp * (displayMetrics.xdpi/DisplayMetrics.DENSITY_DEFAULT)); 
} 

+0

「它不起作用」意味着它不會做改變的價值。 – Vexorei