动态创建相对布局内的视图堆叠

时间:2012-12-17 22:23:09

标签: java android dynamic relativelayout

所以我有一个包含其他相对布局的相对布局。子布局包含我正在从JSON对象解析的各种信息(其具有键/值以及具有子键/ val对象的子JSON数组)。用户通过列表片段导航到每个根JSON对象,该片段片段显示详细信息片段。根详细信息(名称,图像,描述等)显示效果很好。

层次结构:

委员会:

  • 名称
  • 图像
  • 描述
  • 成员:JSON对象数组

成员: - 名称 - 国家 - 图像

这是布局文件:

<code>
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:scrollbars="vertical"
    android:fillViewport="false"
    android:layout_gravity="top"
    android:layout_marginBottom="60dp" >

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/frmCommitteeDetail"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/bg" >

    <!-- Logo and Title Container -->
    <RelativeLayout 
        android:id="@+id/frmImage_Title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="top|center_horizontal">"

        <!-- Committee Logo -->
        <ImageView
            android:id="@+id/imgCommitteeLogo"
            android:contentDescription="@string/content_description"
            android:layout_width="96dp"
            android:layout_height="90dp"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:layout_marginLeft="5dp"
            android:layout_marginTop="26dp"
            android:src="@drawable/default_comm"
            android:background="@drawable/rounded_corners"
            android:padding="4dp" />

        <TextView
            android:id="@+id/txtCommitteeName"
            android:layout_width="200dp"
            android:layout_height="wrap_content"
            android:layout_alignTop="@+id/imgCommitteeLogo"
            android:layout_marginTop="14dp"
            android:layout_marginLeft="8dp"
            android:layout_toRightOf="@+id/imgCommitteeLogo"
            android:background="@drawable/rounded_corners"
            android:gravity="center_horizontal|center_vertical"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:textSize="20sp" />

    </RelativeLayout>

    <!-- Description -->
    <RelativeLayout 
        android:id="@+id/frmComDescription"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="25dp"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"
        android:paddingTop="10dp"
        android:paddingBottom="10dp"
        android:layout_below="@id/frmImage_Title"
        android:background="@drawable/rounded_corners"
        android:gravity="center_horizontal">

        <!-- Webview to have proper text layout -->
        <WebView
            android:id="@+id/txtComDescription"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>

    </RelativeLayout>

    <!-- Contact Infomation -->
    <RelativeLayout 
        android:id="@+id/frmComContact"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="25dp"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"
        android:paddingBottom="20dp"
        android:layout_below="@id/frmComDescription"
        android:background="@drawable/rounded_corners"
        android:gravity="center_horizontal">

        <!-- Contact Title -->
        <TextView
            android:id="@+id/txtComContactTitle"
            android:layout_marginTop="10dp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="top|center_horizontal"
            android:text="@string/contact_info"
            android:textAppearance="?android:attr/textAppearanceLarge" />
        <!-- Separator -->
        <ImageView
            android:contentDescription="@string/content_description"
            android:id="@+id/imgLineComContactTitle"
            android:src="@drawable/cell_divider_line"
            android:layout_below="@id/txtComContactTitle"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>


        <!-- URL -->
        <TextView 
            android:id="@+id/txtComContactURL"
            android:layout_marginTop="10dp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@id/imgLineComContactTitle"
            android:gravity="center_horizontal"
            android:textSize="14sp"
            android:text="www.gooogle.com/url/url/link"/>
        <!-- Email -->
        <TextView
            android:id="@+id/txtComContactEmail"
            android:layout_marginTop="10dp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@id/txtComContactURL"
            android:gravity="center_horizontal"
            android:textSize="14sp"
            android:text="comemail@comsemail.com"/>
        <!-- Name -->
        <TextView
            android:id="@+id/txtComContactName"
            android:layout_marginTop="10dp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@id/txtComContactEmail"
            android:gravity="center_horizontal"
            android:textSize="14sp"
            android:text="John McGee, Contact Guy"/>
        <!-- Number -->
        <TextView
            android:id="@+id/txtComContactNumber"
            android:layout_marginTop="10dp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@id/txtComContactName"
            android:gravity="center_horizontal"
            android:textSize="14sp"
            android:text="(888)-777-1234"/>
        <!-- Address -->
        <TextView
            android:id="@+id/txtComContactAddress"
            android:layout_marginTop="10dp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@id/txtComContactNumber"
            android:gravity="center_horizontal"
            android:textSize="14sp"
            android:text="08 War Memorial Bldg"/>

    </RelativeLayout>

    <!-- Members -->
    <RelativeLayout
        android:id="@+id/frmComMemberList"
        android:layout_below="@id/frmComContact"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"
        android:layout_marginTop="10dp"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:background="@drawable/rounded_corners">

    </RelativeLayout>


</RelativeLayout>
</ScrollView>
</code>

这是现在的片段:

<code>
package v4dev.android.general_assembly.fragment;

import v4dev.android.general_assembly.R;
import v4dev.android.general_assembly.model.CommitteeModel;
import v4dev.android.general_assembly.model.CommitteeModel.Member;
import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

@SuppressLint("ValidFragment")
public class CommitteeDetailFragment extends Fragment
{
    private CommitteeModel Committee = new CommitteeModel();

    @SuppressLint("ValidFragment")
    public CommitteeDetailFragment(CommitteeModel committee)
    {
        this.Committee = committee; 
    }

    // Create View
    @Override
    public View onCreateView(LayoutInflater inflater,  ViewGroup container, Bundle savedInstanceState)
    {
        View view = inflater.inflate(v4dev.android.general_assembly.R.layout.com_detail, container, false);

        ImageView imgLogo = (ImageView) view.findViewById(R.id.imgCommitteeLogo);
        TextView txtCommitteeName = (TextView) view.findViewById(R.id.txtCommitteeName);

        imgLogo.setImageResource(this.Committee.imageID);
        txtCommitteeName.setText(this.Committee.name);

        createDescription(view);
        createContactInfo(view);
        createMembers(view, container);
        return view;
    }// End onCreateView

    // Description
    public void createDescription(View view)
    {   
        // Using a web view since its near impossible to get justified indented text with a native TextView
        // Text View
        WebView txtComDescription       = (WebView)view.findViewById(R.id.txtComDescription);

        // Fill or collapse
        if(!(this.Committee.description.length()==0))
        {
            String Description = "<html><body><p align=\"justify\" style=\"text-indent: 25px\">";
            Description += this.Committee.description;
            Description += "</p></body></html>";
            //txtComDescription.setText(this.Committee.description);
            txtComDescription.loadData(Description,"text/html", "utf-8");
        }
        else
        {
            txtComDescription.setVisibility(View.GONE);
        }
    }
    // Contact Info
    public void createContactInfo(View view)
    {   
        // Text Views
        TextView txtComContactURL       = (TextView)view.findViewById(R.id.txtComContactURL);
        TextView txtComContactEmail     = (TextView)view.findViewById(R.id.txtComContactEmail);
        TextView txtComContactName      = (TextView)view.findViewById(R.id.txtComContactName);
        TextView txtComContactNumber    = (TextView)view.findViewById(R.id.txtComContactNumber);
        TextView txtComContactAddress   = (TextView)view.findViewById(R.id.txtComContactAddress);

        // Fill Data into Layout
        // Collapse Layout if not existent 

        // url
        if(!(this.Committee.url.length() == 0))
        {
            txtComContactURL.setText(this.Committee.url);
        }
        else
        {
            txtComContactURL.setVisibility(View.GONE);
        }
        // contact_email
        if(!(this.Committee.contact_email.length() == 0))
        {
            txtComContactEmail.setText(this.Committee.contact_email);
        }
        else
        {
            txtComContactEmail.setVisibility(View.GONE);
        }
        // contact_name
        if(!(this.Committee.contact_name.length() == 0))
        {
            txtComContactName.setText(this.Committee.contact_name);
        }
        else
        {
            txtComContactName.setVisibility(View.GONE);
        }
        // contact_number
        if(!(this.Committee.contact_number.length() == 0))
        {
            txtComContactNumber.setText(this.Committee.contact_number);
        }
        else
        {
            txtComContactNumber.setVisibility(View.GONE);
        }
        // contact_address
        if(!(this.Committee.contact_address.length() == 0))
        {
            txtComContactAddress.setText(this.Committee.contact_address);
        }
        else
        {
            txtComContactAddress.setVisibility(View.GONE);
        }
    }// end CreateContactInfo()

    // Display all Committee Members
    public void createMembers(View view, ViewGroup container)
    {
        RelativeLayout frmComMemberList = (RelativeLayout) view.findViewById(R.id.frmComMemberList);
        RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
        int id = 1;
        for(Member member: this.Committee.MemberList)
        {

            ImageView imgMember = new ImageView(container.getContext());
            imgMember.setAdjustViewBounds(true);
            imgMember.setMaxHeight(60);
            imgMember.setMaxWidth(60);
            imgMember.setId(id);
            imgMember.setImageResource(member.imageID);
            imgMember.setLayoutParams(lp);

            if(id > 1)
            {
                lp.addRule(RelativeLayout.BELOW, imgMember.getId()  );
                frmComMemberList.addView(imgMember,lp);
            }
            else
            {
                frmComMemberList.addView(imgMember);
            }
            id++;


        }// EndFor
    }
}

</code>

我正在尝试使用相对布局参数动态地将成员“堆叠”在一起。问题是在片段中创建的新视图的id是在运行时生成的。是否有可能将它们“堆叠”起来就好像它们是列表视图一样,或者我最好通过使用实际的列表视图/适配器组合去另一条路线,或者甚至敢说...一个tableview?

提前致谢SO偷看! :)

0 个答案:

没有答案