Java 动态创建按钮不工作
我正在尝试动态创建一个按钮,但我尝试过的任何东西似乎都不起作用。这就是我到目前为止所做的:Java 动态创建按钮不工作,java,android,button,Java,Android,Button,我正在尝试动态创建一个按钮,但我尝试过的任何东西似乎都不起作用。这就是我到目前为止所做的: Button test = new Button(this); test.setText("test"); test.setBackgroundResource(R.color.blue); test.setLayoutParams (new LinearLayout.LayoutParams(60,ViewGroup.LayoutParams.FILL_PARENT)); 这是我的xml文件,它是一个
Button test = new Button(this);
test.setText("test");
test.setBackgroundResource(R.color.blue);
test.setLayoutParams (new LinearLayout.LayoutParams(60,ViewGroup.LayoutParams.FILL_PARENT));
这是我的xml文件,它是一个约束布局,如果它可能与动态创建的线性布局冲突:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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:id="@+id/profileLayout"
android:theme="@android:style/Theme.NoTitleBar"
tools:context="com.example.myfirstapp.MainActivity">
<TextView
android:id="@+id/title"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="My Profile"
android:fontFamily="serif"
android:textColor="@color/green"
android:textAlignment="center"
android:textSize="30dp"
android:layout_marginRight="8dp"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginLeft="8dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="8dp"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_marginBottom="8dp"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintVertical_bias="0.0" />
//Directory Icons
<ImageButton
android:id="@+id/profileDir"
android:layout_width="73dp"
android:layout_height="55dp"
android:backgroundTint="@color/orange"
android:scaleType="fitCenter"
android:src="@drawable/profile_icon"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="8dp"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_marginBottom="8dp"
android:layout_marginLeft="8dp"
app:layout_constraintLeft_toLeftOf="parent"
android:layout_marginRight="8dp"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintVertical_bias="1.0" />
//目录图标
是否将此按钮添加到布局中?
使用
layout.addView(测试)
将测试按钮添加到布局中。好的,所以通过不工作
您的意思是不显示在布局中
这仅仅是因为您创建了一个名为test
的按钮,但它只在内存中,而不在活动的布局中
要动态添加子视图,必须将其添加到父视图中。更详细地说,如果活动的布局(xml)包含容器(LinearLayout
或任何其他容器布局),并且希望将子视图(按钮test
)添加到此容器,则应:
1-使用findViewById()
2-使用addView()
以下是一些例子: 假设您的
xml
包含一个id为myLinearLayout
的LinearLayout
1-使用onCreate()中的findViewById()
绑定它
2-使用addView()
此外,这里还有一个关于动态创建视图的更一般的说明,即使您希望在运行时添加父视图
您可以阅读更多关于addView()
method您所说的“不工作”是什么意思?!指定它只是不显示不,你能详细说明怎么做吗?我添加了这个,现在它崩溃了:LinearLayout布局=(LinearLayout)findViewById(R.id.profileLayout);布局。添加视图(测试);
mLinearLayout = (LinearLayout) findViewById(R.id.myLinearLayout);
Button test = new Button(this);
test.setText("test");
test.setBackgroundResource(R.color.blue);
test.setLayoutParams (new
LinearLayout.LayoutParams(60,ViewGroup.LayoutParams.FILL_PARENT));
mLinearLayout.addView(test); //this will show your button on layout