2014-07-10 88 views
1

我想從xml文件獲取View到普通類,我使用了LayoutInflater,但它僅給出了此視圖的副本,而不是原創(我想設置更改元素)。爲什麼LayoutInflater從xml返回原始視圖的副本,以及如何從xml獲取原始視圖

是否有可能從xml得到原始的佈局?我該如何解決它?

類中,我想佈局

public class Board { 
     private Context context; 
     private View inflated; 
     private RelativeLayout screen; 

     public Board(Context context) { 
      LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      inflated = inflater.inflate(R.layout.board, null); 

      screen = (RelativeLayout) inflated.findViewById(R.id.custom_board_screen); 
      this.context = context; 
     } 

     public TrippleToggleButton getButton(int id) { 
      //return (TrippleToggleButton) inflated.findViewById(id); 
      TrippleToggleButton btn; 
      for(int i=0;i<screen.getChildCount();i++) 
      { 
       View v1 = screen.getChildAt(i); 
       if(v1 instanceof TableLayout) 
       { 
        for(int j=0;j<((TableLayout) v1).getChildCount();j++) 
        { 
         View v2 = ((TableLayout) v1).getChildAt(j); 
         if (v2 instanceof TableRow) 
         { 
          for(int k=0;k<((TableRow) v2).getChildCount();k++) 
          { 
           View v3 = ((TableRow) v2).getChildAt(k); 
           if (v3 instanceof TrippleToggleButton) 
           { 
            Log.i("2014.07.10",screen.getId()+" ; "+v3.getId()); 
//originally id of button are 0-8; inflated 9 - 17 
            if (v3.getId()==id) return (TrippleToggleButton)v3; 
           } 
          } 
         } 
        } 
       } 
      } 
      return null; 
     } 
    } 

XML文件:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/custom_board_screen" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <TableLayout 
     android:id="@+id/custom_board_table" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_centerInParent="true"> 

     <TableRow 
      android:id="@+id/custom_board_table_row1" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content"> 

      <board.TrippleToggleButton 
       /> 
      <View style="@style/DividingVerticalLine"/> 
      <View style="@style/DividingHorizontalLine"/> 
      <board.TrippleToggleButton 
       /> 
      <View style="@style/DividingVerticalLine"/> 
      <board.TrippleToggleButton 
       /> 
     </TableRow> 
     <TableRow 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:background="#ff000000"> 
      <View style="@style/DividingHorizontalLine"/> 
     </TableRow> 

     <TableRow 
      android:id="@+id/custom_board_table_row2" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content"> 

      <board.TrippleToggleButton 
       /> 
      <View style="@style/DividingVerticalLine"/> 
      <View style="@style/DividingHorizontalLine"/> 
      <board.TrippleToggleButton 
       /> 
      <View style="@style/DividingVerticalLine"/> 
      <board.TrippleToggleButton 
       /> 
     </TableRow> 
     <TableRow 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:background="#ff000000"> 
      <View style="@style/DividingHorizontalLine"/> 
     </TableRow> 

     <TableRow 
      android:id="@+id/custom_board_table_row3" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content"> 

      <board.TrippleToggleButton 
       /> 
      <View style="@style/DividingVerticalLine"/> 
      <View style="@style/DividingHorizontalLine"/> 
      <board.TrippleToggleButton 
       /> 
      <View style="@style/DividingVerticalLine"/> 
      <board.TrippleToggleButton 
       /> 
     </TableRow> 




    </TableLayout> 

</RelativeLayout> 

我的自定義按鈕:

public class TrippleToggleButton extends Button implements View.OnClickListener { 
    private Sign actual_state; 
    private final float scale = getContext().getResources().getDisplayMetrics().density; 
    private int dps = 50; 
    private int pixels = (int) (dps * scale + 0.5f); 
    private static int buttonCount = 0; 

    public TrippleToggleButton(Context context) { 
     this(context, null); 
    } 

    public TrippleToggleButton(Context context, AttributeSet attrs) { 
     this(context, attrs, 0); 
    } 

    public TrippleToggleButton(Context context, AttributeSet attrs, int defStyle) { 
     super(context, attrs, defStyle); 
     addFeatures(); 
    } 

    private void addFeatures() { 
     actual_state = Sign.UNCHECKED; 
     setOnClickListener(this); 
     ViewGroup.LayoutParams param = new ViewGroup.LayoutParams(pixels, pixels); 
     setLayoutParams(param); 
     setGravity(Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL); 
     setMinimumHeight(pixels); 
     setMinimumWidth(pixels); 
     setMinHeight(pixels * 2); 
     setMinWidth(pixels * 2); 
     setPadding(pixels/10, pixels/10, pixels/10, pixels/10); 
     setTextSize(pixels); 
     setId(IDAllocator.getNextID()); // this is just a static method which sets ID's starting from 0 
    } 

    private void onUncheckedState() { 

    } 

    public Sign getActual_state() { 
     return actual_state; 
    } 

    public boolean isUnchecked() { 
     return getActual_state() == Sign.UNCHECKED; 
    } 

    public void setState(Sign newState) { 
     actual_state = newState; 
     setText(actual_state.toString()); 
     setClickable(false); 
    } 

    @Override 
    public void onClick(View v) { 
     TrippleToggleButton btn = (TrippleToggleButton) v; 

     if (btn.isUnchecked()) { 
      Toast t = Toast.makeText(getContext(),""+getId(),Toast.LENGTH_SHORT); // it shows correct ID, everything goes ok here 
      t.show(); 
      Container.performAction(btn); 

     } 
    } 
} 
+0

也許這會幫助http://stackoverflow.com/questions/3477422/what-does-layout-inflater-in-android-do – gmo

回答

1

有從XML的沒有 「原創」透視。 xml 描述了的佈局,它不是字面上的佈局層次結構。每當你膨脹一個xml時,inflater使用xml作爲指導建立一個新的佈局層次結構,你可以參考它。如果您想要更新原始虛擬視圖,請將該原始引用傳遞給您的課程,並在其中執行更改,而不是再次膨脹整個內容。

相關問題