更改视图高度和宽度的正确方法 安卓



在这里非常新的Android!我基本上对如何改变视图的高度和宽度感到困惑。目前,我有这样的(片段):

xml(使用相对布局)

<View
    android:id="@+id/homeBackgroundTop"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:layout_centerHorizontal="true"
    android:background="@color/colorAccent" />

java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Display display = getWindowManager().getDefaultDisplay();
    Point size = new Point();
    display.getSize(size);
    int screenWidth = size.x;
    int screenHeight = size.y;
    final float pixelToDp = getResources().getDisplayMetrics().density;
    int screenHeightDp =  (int) (screenHeight * pixelToDp + 0.5f);
    int screenWidthDp =  (int) (screenWidth * pixelToDp + 0.5f);
    View view = findViewById(R.id.homeBackgroundTop);
    view.setLayoutParams(new RelativeLayout.LayoutParams(screenWidthDp, screenHeightDp));
    setContentView(R.layout.activity_main);
}

问题在于正在引起致命错误:

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.view.View.setLayoutParams(android.view.ViewGroup$LayoutParams)' on a null object reference

现在我的问题是,如何通过编程方式正确地更改视图的高度和宽度?我已经看过许多其他答案,但是它们似乎对我有用。

p.s。 RelativeLayout.LayoutParams到底是什么?如果我想更改视图,为什么要调用布局?为什么有时还包含LinearLayout?最后,LayoutParams到底是什么?如果您有任何这些问题的链接,将不胜感激:)谢谢您的帮助!

在您拨打findViewById()之前,您的setContentView(R.layout.activity_main);尚未被调用。因此,由于您的观点尚未附加到您的活动中,因此无法找到任何观点。

在打电话开始或致电findViewById()之前或至少在打电话给setContentView(R.layout.activity_main);

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ...
}

编辑

回答您的P.S。 RelativeLayout.LayoutParams允许您设置基本布局参数,例如视图对象的高度和宽度。您的特定视图(按钮或textView等...)可能从线安排或Relativelayout视图继承。LayoutParams显然特定于不同的布局类型(相对与线性)。目前,我正在寻找一个更具体的答案。让我知道您是否在上面找到任何东西。

尝试此

    ViewGroup.LayoutParams params = view.getLayoutParams();
    params.height = screenHeightDp;
    params.width = screenWidthDp;
    view.setLayoutParams(params);

因此您的onCreate方法就是这样。

   @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Display display = getWindowManager().getDefaultDisplay();
        Point size = new Point();
        display.getSize(size);
        int screenWidth = size.x;
        int screenHeight = size.y;
        final float pixelToDp = getResources().getDisplayMetrics().density;
        int screenHeightDp =  (int) (screenHeight * pixelToDp + 0.5f);
        int screenWidthDp =  (int) (screenWidth * pixelToDp + 0.5f);
        View view = findViewById(R.id.homeBackgroundTop);
        ViewGroup.LayoutParams params = view.getLayoutParams();
        params.height = screenHeightDp;
        params.width = screenWidthDp;
        view.setLayoutParams(params);
    }

在这里呼叫super.Oncreate

我已经对此进行了测试。请让我知道它是否适合您

您正在尝试施放relativeLayout,错误地表明这是错误的。您需要另一种铸造。

顺便说一句,setContentView应就在Super Reference下方。这意味着您正在尝试使用XML的视图元素,而无需首先调用XML。

最新更新