Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/cassandra/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在相对布局内动态创建视图_Java_Android_Dynamic_Android Relativelayout - Fatal编程技术网

Java 在相对布局内动态创建视图

Java 在相对布局内动态创建视图,java,android,dynamic,android-relativelayout,Java,Android,Dynamic,Android Relativelayout,所以我有一个相对布局,其中包含其他相对布局。子布局包含我正在从JSON对象解析的各种信息(该对象具有键/值以及具有子键/值对象的子JSON数组)。用户通过一个列表片段导航到每个根JSON对象,该列表片段显示详细信息片段。根详细信息(名称、图像、描述等)显示得非常好 层次结构: 委员会: 名字 形象 描述 成员:对象的JSON数组 成员: -名字 -陈述 -形象 以下是布局文件: <code> <?xml version="1.0" encoding="utf-8"?>

所以我有一个相对布局,其中包含其他相对布局。子布局包含我正在从JSON对象解析的各种信息(该对象具有键/值以及具有子键/值对象的子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>

包v4dev.android.general_assembly.fragment;
导入v4dev.android.general\u assembly.R;
导入v4dev.android.general_assembly.model.CommitteeModel;
导入v4dev.android.general_assembly.model.CommitteeModel.Member;
导入android.annotation.SuppressLint;
导入android.os.Bundle;
导入android.support.v4.app.Fragment;
导入android.view.LayoutInflater;
导入android.view.view;
导入android.view.ViewGroup;
导入android.webkit.WebView;
导入android.widget.ImageView;
导入android.widget.RelativeLayout;
导入android.widget.TextView;
@SuppressLint(“有效碎片”)
公共类CommitteeDetailFragment扩展了片段
{
私人CommitteeModel委员会=新CommitteeModel();
@SuppressLint(“有效碎片”)
公共委员会(委员会模型委员会)
{
这个委员会=委员会;
}
//创建视图
@凌驾
CreateView上的公共视图(布局、充气机、视图组容器、捆绑包保存状态)
{
视图=充气机。充气(v4dev.android.general\u assembly.R.layout.com\u详细信息,容器,错误);
ImageView imgLogo=(ImageView)view.findViewById(R.id.imgCommitteeLogo);
TextView txtCommitteeName=(TextView)view.findViewById(R.id.txtCommitteeName);
imgLogo.setImageResource(this.Committee.imageID);
txtcommitteName.setText(this.Committee.name);
创建描述(视图);
创建联系人信息(视图);
createMembers(视图、容器);
返回视图;
}//EndOnCreateView
//描述
公共void createDescription(视图)
{   
//使用web视图,因为使用原生TextView几乎不可能获得对齐的缩进文本
//文本视图
WebView txtComDescription=(WebView)view.findviewbyd(R.id.txtComDescription);
//填充或塌陷
if(!(this.Committee.description.length()=0))
{
String Description=“

; Description+=此.Committee.Description; 描述+=“

”; //txtComDescription.setText(this.Committee.description); 加载数据(描述,“文本/html”、“utf-8”); } 其他的 { txtComDescription.setVisibility(View.GONE); } } //联系信息 public void createContactInfo(查看) { //文本视图 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); //将数据填入布局 //折叠布局(如果不存在) //网址 如果(!(this.Committee.url.length()==0)) { txtComContactURL.setText(this.Committee.url); } 其他的 { txtComContactURL.setVisibility(View.GONE); } //电邮联络 如果(!(本委员会联系人_email.length()=0)) { txtComContactEmail.setText(此.Committee.contact_电子邮件); } 其他的 { txtComContactEmail.setVisibility(View.GONE); } //联系人姓名 如果(!(本委员会联系人_name.length()=0)) { txtComContactName.setText(此.Committee.contact_名称); } 其他的 { txtComContactName.setVisibility(View.GONE); } //联络电话 如果(!(本委员会联系方式_number.length()=0)) { txtComContactNumber.setText(本委员会联系电话); } 其他的 { txtComContactNumber.setVisibility(View.GONE); } //联络地址 如果(!(此.委员会.联系人地址.长度()=0)) { txtComContactAddress.setText(本委员会联系地址); } 其他的 { txtComContactAddress.setVisibility(View.GONE); } }//结束CreateContactInfo() //显示所有委员会成员 public void createMembers(视图、视图组容器) { RelativeLayout frmComMemberList=(RelativeLayout)view.findViewById(R.id.frmComMemberList); RelativeLayout.LayoutParams lp=新的RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_内容,RelativeLayout.LayoutParams.WRAP_内容); int-id=1; (成员:本委员会成员名单) { ImageView imgMember=新的ImageView(container.getContext()); imgMember.setAdjustViewBounds(true); imgMember.setMaxHeight(60); imgMember.setMaxWidth(60); imgMember.setId(id)