设置按钮的高度和宽度,包装内容和家长填写 [英] Set button height and width as wrap content and fill parent
问题描述
我开发其中用户应该能够改变一个按钮由pressing其他按钮的apperence一个应用程序。我用四个按钮来设置高度包裹的内容,高度填充母,宽度包裹内容和宽度填充母。
I'm developing a application where the user is supposed to be able to alter the apperence of a button by pressing other buttons. I use four buttons to set height as wrap content, height as fill parent, width as wrap content and width as fill parent.
我GOOGLE了一下,发现使用的LayoutParams虽然这code没有说明,如果宽度的高度是alterd的解决方案。我也得到了错误,说我的IDE无法识别的LayoutParams。什么是做到这一点的最好方法是什么?
I googled a bit and found a solution using LayoutParams although that code didn't specify if height of width was alterd. I also got errors saying my IDE couldn't recognise "LayoutParams". What is the best way to do this?
推荐答案
您需要寻找什么 - 是View.ViewGroup.LayoutParams。
每个都有的LayoutParams恒定值的MATCH_PARENT和WRAP_CONTENT属性。
我有prepared简单code样品,你可以玩:
What you need to look for - is View.ViewGroup.LayoutParams. Every LayoutParams has constant value for MATCH_PARENT and WRAP_CONTENT attributes. I have prepared simple code sample you can play with:
活动:
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:
<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>
这篇关于设置按钮的高度和宽度,包装内容和家长填写的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!