2013-06-27 3 views
1

Я хочу, чтобы преобразовать этот файл XML в пользовательский вид, но он не работает:Преобразование Android настраиваемое представление из XML в Кодекс

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

    <ImageView 
     android:id="@+id/imageView1" 
     android:layout_width="80dp" 
     android:layout_height="80dp" 
     android:src="@drawable/ic_launcher" /> 

    <RelativeLayout 
     android:id="@+id/relativeLayout1" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_toRightOf="@+id/imageView1"> 

     <TextView 
      android:id="@+id/posted_content_user" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="posted_content_user" 
      android:textAppearance="?android:attr/textAppearanceMedium" /> 

     <TextView 
      android:id="@+id/posted_content_date" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignBottom="@+id/posted_content_user" 
      android:layout_alignParentRight="true" 
      android:text="59m" 
      android:textAppearance="?android:attr/textAppearanceSmall" /> 
    </RelativeLayout> 

    <TextView 
     android:id="@+id/textView1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignLeft="@+id/relativeLayout1" 
     android:layout_alignParentRight="true" 
     android:layout_below="@+id/relativeLayout1" 
     android:text="TextView 183091480914809 11rh1h23 k1j2h 3oi12u 3o12h3kj12h3iu12 h3kj12h3 12uy3h12kjh31 i2uy3ijh" /> 

</RelativeLayout> 

Он должен выглядеть следующим образом:

http://i.stack.imgur.com/czXKH.png

Это мой код для сгенерированных View

public void generateView(Context context) { 
    this.setLayoutParams(new RelativeLayout.LayoutParams(
      LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT)); 

    // Contacts Pictures 
    ivContactPictures = new ImageView(context); 
    ivContactPictures.setLayoutParams(new RelativeLayout.LayoutParams(80, 
      80)); 

    // RelativeLayout 
    RelativeLayout relLayoutContent = new RelativeLayout(context); 
    RelativeLayout.LayoutParams relLayoutContentParams = new RelativeLayout.LayoutParams(
      LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT); 
    relLayoutContentParams.addRule(RIGHT_OF, ivContactPictures.getId()); 
    relLayoutContent.setLayoutParams(relLayoutContentParams); 

    // RelativeLayout -> TextView Post User 
    posted_content_user = new TextView(context); 
    posted_content_user.setLayoutParams(new RelativeLayout.LayoutParams(
      LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); 
    // posted_content_user.setTextAppearance(context, 
    // R.attr.textAppearanceMedium); 

    // RelativeLayout -> TextView Time 
    posted_content_date = new TextView(context); 
    RelativeLayout.LayoutParams posted_content_dateParams = new RelativeLayout.LayoutParams(
      LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); 
    // posted_content_user.setTextAppearance(context, 
    // R.attr.textAppearanceSmall); 
    posted_content_dateParams.addRule(ALIGN_BOTTOM, 
      posted_content_user.getId()); 
    posted_content_dateParams.addRule(ALIGN_PARENT_RIGHT); 
    posted_content_date.setLayoutParams(posted_content_dateParams); 

    // textView Content 
    posted_content = new TextView(context); 
    RelativeLayout.LayoutParams posted_contentParams = new RelativeLayout.LayoutParams(
      LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); 
    posted_contentParams.addRule(ALIGN_LEFT, relLayoutContent.getId()); 
    posted_contentParams.addRule(ALIGN_PARENT_RIGHT); 
    posted_contentParams.addRule(BELOW, relLayoutContent.getId()); 
    posted_content.setLayoutParams(posted_contentParams); 

    // Add RelativeLayout 
    this.addView(ivContactPictures); 
    relLayoutContent 
      .addView(posted_content_user); 
    relLayoutContent 
      .addView(posted_content_date); 
    this.addView(relLayoutContent); 
    this.addView(posted_content); 

} 

Результат: http://i.stack.imgur.com/63e3m.png

Что-то не в порядке с моим кодом?

ответ

0

Это не то, как вы создаете пользовательский вид - или, по крайней мере, его очень трудный путь. Вместо этого вы должны создать дочерний элемент представления, который раздувает этот xml в себя как родительский. Это сделает этот класс равным всему содержимому xml-файла. Затем вы можете предоставить любую подходящую функцию getter/setter/other.

+0

Я получил этот учебник на сайте разработчиков Android. Не могли бы вы сказать, как это сделать? Я все еще новичок в разработке Android. – mutokenji

+0

Ознакомьтесь с http://stackoverflow.com/questions/12978298/custom-view-made-of-multiple-views, на котором я задал аналогичный вопрос некоторое время назад. –

Смежные вопросы