2017-04-24 9 views
0

私は動的にボタンを作成しようとしていますが、何も試したことがないようです。これは私がこれまで持っているものです。動的にボタンを作成しないでください

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" /> 
+0

についての詳細を読むことができます!指定する –

+0

それは表示されません – Sea

答えて

0

さて、not workingによって、レイアウトに表示されないことを意味しました。

単にこれは、testという名前のボタンを作成したためですが、アクティビティのレイアウト内ではないだけです。

子ビューを動的に追加するには、親ビューに追加する必要があります。アクティビティのレイアウト(xml)にコンテナ(LinearLayoutまたは他のコンテナレイアウト)が含まれていて、このコンテナに子ビュー(ボタンtest)を追加する必要がある場合は、次のようにする必要があります。

1-バインドfindViewById()

2-使用して、あなたのコード内のコンテナ(親ビュー)は、ここでaddView()


を使用して、親ビューに子ビューを追加し、いくつかの例です:

補遺それバインドmyLinearLayout

の1-また、ここでaddView()

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 

を使用して、それにあなたのbuttonを追加

2 -

mLinearLayout = (LinearLayout) findViewById(R.id.myLinearLayout); 

onCreate()findViewById()を使用して:あなたの xmlは、IDと LinearLayoutが含まれているOSE動的なcreについてのより一般的な exampleです実行時に親ビューを追加したい場合でも、atedビューを使用できます。

そして、あなたは、あなたが「動作しない」とはどういう意味ですか?addView()方法here

0

あなたにこのボタンを追加しましたレイアウト? layout.addView(test);を使用してレイアウトにテストボタンを追加します。

+0

いいえ、あなたはこれを行う方法を詳しく説明できますか? – Sea

+0

これを追加して、クラッシュしました。 LinearLayout layout =(LinearLayout)findViewById(R.id.profileLayout); layout.addView(test); – Sea

+1

私を見せて、なぜそれはクラッシュするのですか?どんな例外? –

関連する問題