如何从我的Activity以编程方式创建此XML?

时间:2016-05-10 05:25:11

标签: android xml

所以我知道如何以编程方式创建这些元素中的每一个或所有元素,但我不知道如何将这些LinearLayout与权重等放在一起,有人可能会以编程方式生成以下XML来帮助我吗?

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          xmlns:tools="http://schemas.android.com/tools"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:background="@drawable/background_main"
          android:orientation="vertical"
          android:paddingBottom="@dimen/activity_vertical_margin"
          android:paddingLeft="@dimen/activity_horizontal_margin"
          android:paddingRight="@dimen/activity_horizontal_margin"
          android:paddingTop="@dimen/activity_vertical_margin"
          android:id="@+id/main"
          tools:context=".MainActivity"
          android:weightSum="3">

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_weight="1"
    android:orientation="horizontal">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <Button
            android:id="@+id/sa_id"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:background="@drawable/sa_id_small"
            android:onClick="onSAIDClicked"
            android:visibility="invisible"
            android:layout_marginTop="10dp"
            android:layout_marginBottom="10dp"
            android:layout_marginLeft="20dp"
            android:layout_marginRight="20dp"/>
    </RelativeLayout>

</LinearLayout>

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_weight="1"
    android:orientation="horizontal">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <EditText
            android:id="@+id/phone_number"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:inputType="phone"
            android:singleLine="true"
            android:textColor="#FFF"
            android:textColorHint="#FFF"/>

        <EditText
            android:id="@+id/pin"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/phone_number"
            android:layout_centerHorizontal="true"
            android:hint="Pin Code"
            android:imeOptions="actionDone"
            android:inputType="numberPassword"
            android:maxLength="4"
            android:singleLine="true"
            android:textColor="#FFF"
            android:textColorHint="#FFF"/>

        <Button
            android:id="@+id/login"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/pin"
            android:layout_centerHorizontal="true"
            android:onClick="onButtonClicked"
            android:text="Register"/>

        <Button
            android:id="@+id/usa_drivers"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:background="@drawable/usa_drivers_dc_small"
            android:onClick="onUSADriversClicked"
            android:visibility="invisible"
            android:layout_marginTop="10dp"
            android:layout_marginBottom="10dp"
            android:layout_marginLeft="20dp"
            android:layout_marginRight="20dp"/>
    </RelativeLayout>

</LinearLayout>

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_weight="1"
    android:orientation="horizontal">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <Button
            android:id="@+id/usa_passport"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:background="@drawable/usa_passport_small"
            android:onClick="onUSAPassportClicked"
            android:visibility="invisible"
            android:layout_marginTop="10dp"
            android:layout_marginBottom="10dp"
            android:layout_marginLeft="20dp"
            android:layout_marginRight="20dp"/>
    </RelativeLayout>

</LinearLayout>

谢谢, 的Wihan

4 个答案:

答案 0 :(得分:2)

您可以使用以下示例:

    LinearLayout.LayoutParams childParam1 = new LinearLayout.LayoutParams(0,LinearLayout.LayoutParams.MATCH_PARENT);
childParam1.weight = 0.3f;
child1.setLayoutParams(childParam1);

LinearLayout.LayoutParams childParam2 = new LinearLayout.LayoutParams(0,LinearLayout.LayoutParams.MATCH_PARENT);
childParam1.weight = 0.7f;
child2.setLayoutParams(childParam2);

parent.setWeightSum(1f);
parent.addView(child1);
parent.addView(child2);

答案 1 :(得分:0)

你可以这样做。例如:

        // Create a new RelativeLayout
        RelativeLayout relativeLayout = new RelativeLayout(this);

        // Defining the RelativeLayout layout parameters.
        // In this case I want to fill its parent
        RelativeLayout.LayoutParams layoutParams= new RelativeLayout.LayoutParams(
                RelativeLayout.LayoutParams.FILL_PARENT,
                RelativeLayout.LayoutParams.FILL_PARENT);

        setContentView(relativeLayout, layoutParams);

答案 2 :(得分:0)

要在LinearLayout上以编程方式设置布局权重和其他布局设置,请在LinearLayout.LayoutParams的实例上设置属性。布局权重是构造函数的第三个参数。

LinearLayout layout = new LinearLayout(context);
LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(
                             LayoutParams.FILL_PARENT,
                             LayoutParams.FILL_PARENT, 1.0f);
layout.setLayoutParams(param);

LinearLayout API Docs将提供有关用于对XML中的数据进行编码的各种属性和设置器的更多详细信息。

通常,根据您要执行的操作,最好在XML中定义布局,提供相关元素ID,然后使用findViewById(R.id.yourElementId)以编程方式访问它们。

如果您对使用哪种方法或在XML和Java API之间移动存在疑问,那么文档就是第一站。请查看ButtonRelativeLayout的文档。

答案 3 :(得分:0)

你可以这样轻松地做到这一点:

在以下示例Switch中,TextViewRelative View是根据活动创建的,您不需要使用 setContentView(R.layout.activity_main) 只需在super.onCreate(savedInstanceState)之后使用此代码并执行。

//UI Views
Switch btn_toggle_location, btn_toggle_state;
TextView txt_log;
RelativeLayout ad_container_top;
RelativeLayout ad_container_bottom;
RelativeLayout main_layout;

//Define properties of Switch 1
btn_toggle_location = new Switch(this);
btn_toggle_location.setChecked(true);
btn_toggle_location.setClickable(true);
btn_toggle_location.setId(1);

//Define properties of Switch 2
btn_toggle_state = new Switch(this);
btn_toggle_state.setChecked(true);
btn_toggle_state.setClickable(true);
btn_toggle_state.setId(2);

//Define properties of TextView 
txt_log = new TextView(this);
txt_log.setMovementMethod(new ScrollingMovementMethod());
txt_log.setId(3);

//Define Layout parameters
RelativeLayout.LayoutParams params_btn_location = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
        ViewGroup.LayoutParams.WRAP_CONTENT);
params_btn_location.addRule(RelativeLayout.CENTER_IN_PARENT);
params_btn_location.setMargins(10, 0, 10, 0);
btn_toggle_location.setLayoutParams(params_btn_location);

RelativeLayout.LayoutParams params_btn_state = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
        ViewGroup.LayoutParams.WRAP_CONTENT);
params_btn_state.setMargins(10, 10, 10, 0);
params_btn_state.addRule(RelativeLayout.BELOW, btn_toggle_location.getId());
btn_toggle_state.setLayoutParams(params_btn_state);

RelativeLayout.LayoutParams params_log = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
        ViewGroup.LayoutParams.WRAP_CONTENT);
params_log.addRule(RelativeLayout.BELOW, btn_toggle_state.getId());
params_log.setMargins(10, 0, 10, 0);
txt_log.setLayoutParams(params_log);

//Add switch click listeners
btn_toggle_location.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        ad_container_bottom.removeAllViews();
        ad_container_top.removeAllViews();
        txt_log.setText("");
        if (isChecked) {


        } else {

        }
    }
});

btn_toggle_state.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        txt_log.setText("");
        if (isChecked) {

        } else {

        }
    }
});

main_layout = new RelativeLayout(this);
RelativeLayout.LayoutParams main_lp = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
        ViewGroup.LayoutParams.MATCH_PARENT);

ad_container_top = new RelativeLayout(this);
RelativeLayout.LayoutParams layout_top = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
        ViewGroup.LayoutParams.WRAP_CONTENT);
layout_top.addRule(RelativeLayout.CENTER_VERTICAL);
layout_top.addRule(RelativeLayout.CENTER_HORIZONTAL);
layout_top.addRule(RelativeLayout.ALIGN_PARENT_TOP);
ad_container_top.setLayoutParams(layout_top);

ad_container_bottom = new RelativeLayout(this);
RelativeLayout.LayoutParams layout_bottom = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
        ViewGroup.LayoutParams.WRAP_CONTENT);
layout_bottom.addRule(RelativeLayout.CENTER_VERTICAL);
layout_bottom.addRule(RelativeLayout.CENTER_HORIZONTAL);
layout_bottom.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
ad_container_bottom.setLayoutParams(layout_bottom);

//Set view visibility
ad_container_bottom.setVisibility(View.VISIBLE);
ad_container_top.setVisibility(View.VISIBLE);


//Finally add all views to main layout here
main_layout.addView(btn_toggle_location, params_btn_location);
main_layout.addView(btn_toggle_state, params_btn_state);
main_layout.addView(txt_log, params_log);
main_layout.addView(ad_container_top, layout_top);
main_layout.addView(ad_container_bottom, layout_bottom);
main_layout.setBackgroundColor(Color.WHITE);
this.addContentView(main_layout, main_lp);