Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/225.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/14.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
Android 如何从活动中以编程方式创建此XML?_Android_Xml - Fatal编程技术网

Android 如何从活动中以编程方式创建此XML?

Android 如何从活动中以编程方式创建此XML?,android,xml,Android,Xml,因此,我知道如何以编程方式创建每个或所有这些元素,但我不确定如何使用权重等放置这些线性布局,是否有人可以帮助我以编程方式生成以下XML <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"

因此,我知道如何以编程方式创建每个或所有这些元素,但我不确定如何使用权重等放置这些线性布局,是否有人可以帮助我以编程方式生成以下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>

谢谢,
你可以这样做。例如:

        // 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);

你可以这样做。例如:

        // 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);

您可以使用以下示例:

    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);

您可以使用以下示例:

    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);

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

LinearLayout layout = new LinearLayout(context);
LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(
                             LayoutParams.FILL_PARENT,
                             LayoutParams.FILL_PARENT, 1.0f);
layout.setLayoutParams(param);
将提供有关用于编码XML数据的各种属性和设置器的更多详细信息

通常,根据您要做的事情,最好在XML中定义布局,给出相关元素id,然后使用
findViewById(R.id.yourElementId)
以编程方式访问它们


如果您对使用哪种方法或在XML和JavaAPI之间移动有疑问,那么文档是一个很好的第一站。查看和的文档。

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

LinearLayout layout = new LinearLayout(context);
LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(
                             LayoutParams.FILL_PARENT,
                             LayoutParams.FILL_PARENT, 1.0f);
layout.setLayoutParams(param);
将提供有关用于编码XML数据的各种属性和设置器的更多详细信息

通常,根据您要做的事情,最好在XML中定义布局,给出相关元素id,然后使用
findViewById(R.id.yourElementId)
以编程方式访问它们


如果您对使用哪种方法或在XML和JavaAPI之间移动有疑问,那么文档是一个很好的第一站。请查看和的文档。

您可以像这样轻松地执行此操作:

在下面的示例中,
切换
文本视图
相对视图
是通过活动编程创建的,您不需要使用
setContentView(R.layout.activity\u 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);

您可以像这样轻松地执行此操作:

在下面的示例中,
切换
文本视图
相对视图
是通过活动编程创建的,您不需要使用
setContentView(R.layout.activity\u 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);

为什么要用编程的方式来做呢?因为我需要从数据库中动态地绘制这些元素。。。为什么重要?为什么?画画?你想画什么?你到底想实现什么?为什么要通过编程来实现?因为我需要从数据库中动态地绘制这些元素。。。为什么重要?为什么?画画?你想画什么?你到底想要实现什么?