2013-07-02 53 views
1

我已經創建了一個自定義TextView,並且我想在某個特定位置動態添加這些TextViews,就像在EditText下面一樣。有人可以告訴我該怎麼做?我想通過java而不是XML動態地添加我的自定義textView,如何使用addView()爲此? XML的 enter image description here在某個位置添加自定義TextView?

部分是(我的XML文件是非常大的規模):

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/mainStaffScrollView" 
android:layout_width="match_parent" 
android:layout_height="match_parent" > 

<LinearLayout 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:orientation="vertical"> 

<!-- buttons,spinners,edittexts etc--> 

<Button 
    android:id="@+id/specialisationStaffSpinner" 
    style="?android:attr/spinnerStyle" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_gravity="center" 
    android:layout_marginTop="5dp" 
    android:gravity="left|center_vertical" 
    android:text="@string/names" /> 

<!-- here i want to add my custom view--> 

</LinearLayout> 
</ScrollView> 
+0

這是可以做到的,但你要發佈現有佈局的更多細節。它是一個LinearLayout嗎?一個RelativeLayout?如果它來自XML,你可以發佈xml嗎? – Martin

+2

在你想添加自定義textview的地方放置一個空佈局視圖.....通過它的id將此佈局放入你的活動中,並將自定義textview添加到你在xml中定義的空佈局中。 ....... – Aarun

回答

1

您可以在要插入自定義TextView的,然後在Java代碼中引用它的位置在LinearLayout中添加一個空的LinearLayout,然後使用addView()。祝你好運! :)

+0

這看起來不錯,讓我試試這個 –

0

使用相對佈局爲父它給你的權力,以達到你想要什麼 創建佈局PARAMS並使用addRule函數

這樣

RelativeLayout.LayoutParams lp=new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); 
     lp.addRule(RelativeLayout.BELOW, ButtonId); 

     adsLayout.setLayoutParams(lp); 
3
  • 把一個空的佈局視圖在您想通過其ID
  • 到此佈局添加自定義 的TextView
  • 進入你的活動,並添加自定義的TextView到您的 XML
  • 定義空佈局的地方
0

如果您使用RelativeLayout,則可以通過3步輕鬆完成此操作。

  1. 確保頂部和底部按鈕都有有效的ID。

  2. 使用RelativeLayout.LayoutParams初始化並指定位置。

RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT); params.addRule(RelativeLayout.BELOW, topButton.getId()); params.addRule(RelativeLayout.ABOVE, bottomButton.getId());

3.添加視圖。

targetContainer.addView(customTextView, params); 

雖然沒有使用RelativeLayout會更困難。

+0

我沒有使用相對佈局,我用我的xml完成​​了線性佈局 –

+2

如果您事先知道需要添加自定義文本視圖的位置,請使用R4chi7的答案。 – Stormbringer

0

嘗試這種方式

LinearLayout linearLayout = (LinearLayout) findViewById(R.id.mylayout) 
TextView txt1 = new TextView(this, R.drawable.customexml); 
linearLayout.addView(txt1); 
+0

這不會添加視圖所顯示的圖像...我已經嘗試過此 –

0

只是做了開始與編輯文本,找到代碼中的引用和更新它和它的可見性設置爲View.VISIBLE

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/mainStaffScrollView" 
android:layout_width="match_parent" 
android:layout_height="match_parent" > 

<LinearLayout 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:orientation="vertical"> 

<!-- buttons,spinners,edittexts etc--> 

<Button 
    android:id="@+id/specialisationStaffSpinner" 
    style="?android:attr/spinnerStyle" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_gravity="center" 
    android:layout_marginTop="5dp" 
    android:gravity="left|center_vertical" 
    android:text="@string/names" /> 

    <View 
    android:visiblity="gone" 
    android:id="+id/customView 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    </View> 


</LinearLayout> 
</ScrollView> 
相關問題