Java – sets the button height and width to wrap the contents and populate the parent

I'm developing an application where users should be able to change the appearance of buttons by pressing other buttons I use four buttons to set the height to wrap, the height to fill the parent, the width to wrap, and the width to fill the parent

I googled and found a solution using layoutparams, although the code didn't specify whether to change the height of the width I also made the mistake that my ide doesn't recognize "layoutparams" What's the best way to do this?

Solution

What you need to look for is view ViewGroup. LayoutParams.

Activities:

package com.example.stack2;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;

public class MainActivity extends Activity implements OnClickListener{

    Button test;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button b = (Button)findViewById(R.id.button1);
        b.setOnClickListener(this);
        b = (Button)findViewById(R.id.button2);
        b.setOnClickListener(this);
        b = (Button)findViewById(R.id.button3);
        b.setOnClickListener(this);
        b = (Button)findViewById(R.id.button4);
        b.setOnClickListener(this);
        test = (Button)findViewById(R.id.test);
    }
    public void onClick(View v)
    {
        LayoutParams lp = test.getLayoutParams();
        if(v.getId() == R.id.button1)
        {
            lp.height = LayoutParams.WRAP_CONTENT;
        }else if(v.getId() == R.id.button2){
            lp.width = LayoutParams.WRAP_CONTENT;
        }else if(v.getId() == R.id.button3){
            lp.height = LayoutParams.MATCH_PARENT;
        }else if(v.getId() == R.id.button4){
            lp.width = LayoutParams.MATCH_PARENT;
        }
        test.setLayoutParams(lp);
    }
}

Layout XML:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <LinearLayout 
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:orientation="horizontal">

        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="h_wc" />

        <Button
            android:id="@+id/button2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="w_wc" />

        <Button
            android:id="@+id/button3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="h_fp" />

        <Button
            android:id="@+id/button4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="h_fp" />

    </LinearLayout>

    <Button
        android:id="@+id/test"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="test" />

</LinearLayout>
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>