Setting the height and width of buttons as the contents of the wrapper and filling the parent element

I am developing an application in which the user should be able to change the application of a button by pressing other buttons. I use four buttons to set the height as the content of the wrapping, the height of how to fill the parent, the width of how the content and the width of the wrapper, how to fill the parent element.

I searched Google a bit and found a solution using LayoutParams, although this code did not indicate whether the width height was changed. I also got errors stating that my IDE could not recognize "LayoutParams". What is the best way to do this?

+6
source share
2 answers

What you need to look for is View.ViewGroup.LayoutParams. Each LayoutParams has a constant value for the MATCH_PARENT and WRAP_CONTENT attributes. I have prepared a simple code example with which you can play:

Activity:

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); } } 

Xml layout:

 <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> 
+10
source

Using the code below, you can easily change the height and width.

 btn = new Button(Activity.this); btn.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT)); btn.setText("\t\t" + lbl + "\t\t\t "); btn.setBackgroundResource(R.drawable.blue_button); btn.setwidth(100); 
0
source

Source: https://habr.com/ru/post/921527/


All Articles