3 回答
TA贡献1852条经验 获得超1个赞
您可以获取 View 的 LayoutParams 对象,然后设置 Height 和 Width 以动态查看,如下面的代码:
View view = (View)findViewById(R.id. myRectangleView);
LayoutParms layoutParams = view.getLayoutParms();
layoutParms.height = 200;
layoutParms.width = 300;
TA贡献1829条经验 获得超9个赞
我已经在我的身上尝试过这个解决方案。希望它对你没问题。
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.Display;
import android.view.View;
import android.view.ViewGroup;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
View view = (View) findViewById(R.id.myRectangleView);
ViewGroup.LayoutParams layoutParams = view.getLayoutParams();
layoutParams.height = 500;
Display display = getWindowManager().getDefaultDisplay();
DisplayMetrics outMetrics = new DisplayMetrics();
display.getMetrics(outMetrics);
float density = getResources().getDisplayMetrics().density;
float dpHeight = outMetrics.heightPixels / density;
if ((int) dpHeight <= 400) {
layoutParams.height = 32;
} else if ((int) dpHeight > 400 && dpHeight <= 720) {
layoutParams.height = 50;
} else if ((int) dpHeight > 720) {
layoutParams.height = 90;
}
}
}
TA贡献1777条经验 获得超3个赞
你可以使用这个:
public static int getScreenHeightInDP(Context context) {
DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics();
float screenHeightInDP = displayMetrics.heightPixels / displayMetrics.density;
return Math.round(screenHeightInDP);
}
添加回答
举报