-1

我是 android 新手,我已经为这个问题做了很多谷歌搜索。实际上我想在 android 的代码中创建下面的 UI:

<RelativeLayout
        android:id="@+id/relativeLayout2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:background="@drawable/tab_background">

       <RelativeLayout
            android:id="@+id/event_1"
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:background="@drawable/color_001">

        <TextView
            android:id="@+id/txt_event_1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true"
            android:layout_margin="1dp"
            android:gravity="center"
            android:text="j1"
            android:textStyle="bold"
            android:textColor="#ffffff" />
        </RelativeLayout>  
</RelativeLayout>  

有人可以帮我吗?

4

4 回答 4

1

使用此代码。

RelativeLayout layout1= new RelativeLayout(this);
LayoutParams param1= new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT);

RelativeLayout layout2= new RelativeLayout(this);
LayoutParams param2= new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);

TextView text= new TextView(this);
text.setText("your text");
layout2.addView(text, param2);

layout1.addView(layout2, param1);
setContentView(layout1);
于 2013-06-24T11:22:59.243 回答
1
    RelativeLayout.LayoutParams childVw = new RelativeLayout.LayoutParams(40, 40);
    TextView textVw = new TextView(this);
    textVw.setLayoutParams(childVw);

然后将其添加到您的主要相对布局

于 2013-06-24T11:31:40.063 回答
1

我已经根据您的 xml 布局编写了一个快速示例代码。我尝试通过使用注释来说明清楚,但是如果您不理解某些内容,我可以尝试再解释一下。

// Creating the outer RelativeLayout which has id "relativeLayout2" in your xml layout.
RelativeLayout outerRelativeLayout = new RelativeLayout(context);

// ------------------------------------------------------------------

// Creating the inner RelativeLayout which has id "event_1" in your xml layout.
RelativeLayout innerRelativeLayout = new RelativeLayout(context);

// Creating the TextView which has id "txt_event_1" in your xml layout.
TextView textView = new TextView(context);
textView.setGravity(Gravity.CENTER);
textView.setText("j1");

// Defining the layout parameters of the TextView
RelativeLayout.LayoutParams textViewParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
textViewParams.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE);
textViewParams.addRule(RelativeLayout.CENTER_VERTICAL, RelativeLayout.TRUE);

// Adding the TextView to the inner RelativeLayout as a child
innerRelativeLayout.addView(textView, new RelativeLayout.LayoutParams(
            ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));

// ------------------------------------------------------------------

// Adding the inner RelativeLayout to the outer RelativeLayout as a child
outerRelativeLayout.addView(innerRelativeLayout, new RelativeLayout.LayoutParams(convertDptoPx(40), convertDptoPx(40)));

// ------------------------------------------------------------------

// Defining the layout parameters of the outer RelativeLayout
RelativeLayout.LayoutParams rootParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
rootParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);

// Set the layout parameters associated with the outer RelativeLayout.
outerRelativeLayout.setLayoutParams(rootParams);

虽然此代码片段不包含在您的 xml 布局中定义的一些 xml 属性,但您可以在视图的文档中找到相关方法。

例如,如果您查看 TextView 文档 ( http://developer.android.com/reference/android/widget/TextView.html ),您可以看到显示属性名称和相关方法的“XML 属性”表。

于 2013-06-24T11:31:43.123 回答
1
 setContentView(R.layout.layoutfilename);


    RelativeLayout layout1= (RelativeLayout)findViewById(R.id.yourlayoutid);
    LayoutParams param1= new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT);

    RelativeLayout layout2= new RelativeLayout(this);
    LayoutParams param2= new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);

    TextView text= new TextView(this);
    text.setText("your text");
    layout2.addView(text, param2);


    layout1.addView(layout2);
于 2013-06-24T13:52:35.070 回答