在oncreate方法之外使用layoutparams并不起作用

时间:2017-07-26 09:11:46

标签: java android xml layoutparams

当我点击一个按钮时,我试图将LinearLayout高度更改为父布局的50%。我使用LinearLayout.LayoutParams对它进行了试验,但如果我在onCreate外部的方法中按钮时调用它,它就不会改变布局的高度。但是,如果我将LinearLayout.LayoutParams保留在onCreate中,那么它可以工作。当我点击按钮时,我该怎么做?

此代码有效:

public class ViewReport extends AppCompatActivity {

Button button;
LinearLayout layout;

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

    button=(Button) findViewById(R.id.button);
    layout=(LinearLayout) findViewById(R.id.fold);

    final LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) layout.getLayoutParams();
    lp.height = 0;
    lp.width=LinearLayout.LayoutParams.MATCH_PARENT;
    lp.weight=0.5f;

}
}

此代码无效

public class ViewReport extends AppCompatActivity {

Button button;
LinearLayout layout;

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

    button=(Button) findViewById(R.id.button);
    layout=(LinearLayout) findViewById(R.id.fold);
}


public  void Click(View view)
{
    Toast.makeText(getApplicationContext(),"tapped",Toast.LENGTH_LONG).show();
    LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) layout.getLayoutParams();
    lp.height = 0;
    lp.width=LinearLayout.LayoutParams.MATCH_PARENT;
    lp.weight=0.5f;
}
}

布局XML

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="me.imbishal.imageblur.ViewReport"
android:weightSum="1">

<LinearLayout
    android:id="@+id/fold"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1"
    android:background="@drawable/profilepageback"
    android:orientation="vertical">

    <EditText
        android:id="@+id/editText4"
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:layout_marginBottom="3dp"
        android:background="@drawable/my_button"
        android:ems="10"
        android:inputType="textPersonName" />

    <EditText
        android:id="@+id/editText3"
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:layout_marginBottom="5dp"
        android:background="@drawable/my_button"
        android:ems="10"
        android:inputType="textPersonName" />

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:background="@drawable/just_border"
        android:onClick="Click"
        android:text="Button" />
</LinearLayout>

1 个答案:

答案 0 :(得分:0)

您已更改了LayoutParams但尚未设置它们。添加此行

layout.setLayoutParams(lp);