动态更改childView属性

时间:2014-01-22 06:24:26

标签: android custom-view

我有一个自定义视图,我在其中放置了一些TextView,我必须以不同方式更改每个子视图的布局属性。我无法通过xml,

更改textview属性

这是我的xml:

<com.squareup.timessquare.CalendarRowView
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:paddingBottom="@dimen/calendar_day_headers_paddingbottom">

      <TextView
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          style="@style/CalendarCell.DayHeader"
          />
      <TextView
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          style="@style/CalendarCell.DayHeader"
          />
      <TextView
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          style="@style/CalendarCell.DayHeader"
          />
      <TextView
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          style="@style/CalendarCell.DayHeader"
          />
      <TextView
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          style="@style/CalendarCell.DayHeader"
          />
      <TextView
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          style="@style/CalendarCell.DayHeader"
          />
      <TextView
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          style="@style/CalendarCell.DayHeader"
          />
    </com.squareup.timessquare.CalendarRowView>

上面是xml文件,其中定义了8个textview我希望每个textview都有不同的宽度,

以下是该代码的java代码...

public static MonthView create(ViewGroup parent, LayoutInflater inflater,
      DateFormat weekdayNameFormat, Listener listener, Calendar today) 
  {
    final MonthView view = (MonthView) inflater.inflate(R.layout.month, parent, false);
    final int originalDayOfWeek = today.get(Calendar.DAY_OF_WEEK);

    int firstDayOfWeek = today.getFirstDayOfWeek();
    final CalendarRowView headerRow = (CalendarRowView) view.grid.getChildAt(0);
    for (int offset = 0; offset < 7; offset++) 
    {
      today.set(Calendar.DAY_OF_WEEK, firstDayOfWeek + offset);
      final TextView **textView** = (TextView) headerRow.getChildAt(offset);
      textView.setText(weekdayNameFormat.format(today.getTime()));
    }
    today.set(Calendar.DAY_OF_WEEK, originalDayOfWeek);
    view.listener = listener;
    return view;
  }

1 个答案:

答案 0 :(得分:0)

使用带有XML的Inflater将您的布局限制为XML中的内容。

要动态更改布局属性,您还必须动态创建布局, 例如:

CalendarRowView calenderViewGroup = new CalendarRowView(thisContext);
int widthNeeded = 300;  // in pixels
TextView someTextView = new TextView(thisContext);
someTextView.setText("this is my text");
someTextView.setWidth(widthNeeded );
someTextView.setLayoutParams(new LayoutParams(
        LayoutParams.FILL_PARENT,
        LayoutParams.WRAP_CONTENT));

calenderViewGroup.addView(someTextView);

有关更多可用方法,请参阅公开方法此处http://developer.android.com/reference/android/widget/TextView.html