2017-08-02 90 views
0

時,我有一個XML佈局看起來像這樣:使用「toRightOf」編程充氣佈局

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/parent" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal"> 
    <TextView 
     android:layout_width="40dp" 
     android:layout_height="40dp" 
     android:layout_weight="0.1" 
     android:text="1"/> 
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:id="@+id/child" 
     android:layout_weight="1" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:gravity="center"> 

     <!-- Define these into code 
     <TextView android:id="@+id/first" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:textSize="20sp" 
      android:text="1"/> 
     <TextView android:id="@+id/second" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_toRightOf="@id/first" 
      android:layout_marginLeft="5dp" 
      android:textSize="20sp" 
      android:text="1"/>--> 

    </RelativeLayout> 
    <TextView 
     android:layout_width="40dp" 
     android:layout_height="40dp" 
     android:layout_weight="0.1" 
     android:text="1"/> 
</LinearLayout> 

LinearLayoutTextView s的註定不會被改變。 RelativeLayout也由TextView組成,它們彼此對齊5dp。與父佈局的不同,它們需要在我的Java活動中定義。這是我曾嘗試:

private View getGeneratedView(String[] texts) { 
    LayoutInflater inflater = 
      (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    View parentLayout = inflater.inflate(R.layout.parent, null); 
    RelativeLayout childLayout = (RelativeLayout) parentLayout.findViewById(R.id.child); 

    Integer lastViewId = null; 
    for (String text: texts) { 
     TextView displayedText = new TextView(this); 
     displayedText.setText(text); 
     displayedText.setTextSize(20); 

     RelativeLayout.LayoutParams params = 
       new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, 
         RelativeLayout.LayoutParams.WRAP_CONTENT); 
     if (lastViewId != null) { 
      params.addRule(RelativeLayout.RIGHT_OF, lastViewId); 
      params.leftMargin = 5; 
     } 
     displayedText.setLayoutParams(params); 

     lastViewId = displayedText.getId(); 
     childLayout.addView(displayedText); 
    } 

    return parentLayout; 
} 

但是上面的代碼確實是把所有的TextView S在中心,一個在另一個的上面,像這樣: layout processed inside Java(參數textsnew String[] {"1", "1", "1", "1"}

而這正是我試圖得到: xml defined layout

任何人都可以指出我在做什麼錯?

回答

1

即使您爲texts中的每個字符串生成新的TextView,也不會爲其生成一個ID。因此,displayedText.getId()將始終爲-1(View.NO_ID),並且lastViewId將永遠不會是-1。

要更正代碼,您需要爲每個生成的TextView生成一個新的ID。使用API​​ 17+,您可以使用View.generateViewId()來執行此操作。

private View getGeneratedView(String[] texts) { 
    LayoutInflater inflater = 
      (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    View parentLayout = inflater.inflate(R.layout.parent, null); 
    RelativeLayout childLayout = (RelativeLayout) parentLayout.findViewById(R.id.child); 

    Integer lastViewId = null; 
    for (String text : texts) { 
     TextView displayedText = new TextView(this); 
     // Need API 17+ for generateViewId 
     displayedText.setId(View.generateViewId()); 
     displayedText.setText(text); 
     displayedText.setTextSize(20); 

     RelativeLayout.LayoutParams params = 
       new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, 
         RelativeLayout.LayoutParams.WRAP_CONTENT); 
     if (lastViewId != null) { 
      params.addRule(RelativeLayout.RIGHT_OF, lastViewId); 
      params.leftMargin = 5; 
     } 
     displayedText.setLayoutParams(params); 

     lastViewId = displayedText.getId(); 
     childLayout.addView(displayedText); 
    } 

    return parentLayout; 
} 
+0

非常感謝,你現在救了我:) –