2017-04-24 87 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()


在這裏你代碼容器(父視圖)是一些例子:

增刊您button它使用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 

而且,這裏將其綁定使用findViewById()onCreate()

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

2-加myLinearLayout

1:OSE你xml包含ID的LinearLayout是關於動態cre的更一般的example即使您想在運行時添加父視圖,也可以使用視圖。

而且你可以閱讀更多有關addView()方法here

0

你添加此按鈕,佈局? 使用layout.addView(test);將測試按鈕添加到佈局。

+0

不,你可以詳細說明如何做到這一點? – Sea

+0

我把它添加到它,現在它崩潰: LinearLayout layout =(LinearLayout)findViewById(R.id.profileLayout); layout.addView(test); – Sea

+1

給我看,爲什麼會崩潰?有什麼異常? –