简体   繁体   English

在Android中以编程方式设置EditText的layout_width

[英]Programatically set layout_width of EditText in Android

So I have an application that dynamically adds a table row every time the "Add Row" button is pressed. 因此,我有一个应用程序,每当按下“添加行”按钮时,就会动态添加一个表行。 The code attached is the code that runs when "Add Row" is pressed. 附带的代码是按下“添加行”时运行的代码。 What I am trying to do is have 3 EditTexts next to each other on the same row. 我想做的是在同一行上彼此相邻的3个EditText。 In order to do that, I need to change the Layout_Width of each EditText so the first EditText doesn't cut the other two off the screen. 为此,我需要更改每个EditText的Layout_Width,以便第一个EditText不会将其他两个EditText截断。 I can't seem to figure out a way to properly do this and was wondering if anyone would help me out. 我似乎无法找到一种正确执行此操作的方法,并且想知道是否有人会帮助我。 After I figure out how to do that, the next step is to adjust the layout_width according to the screen size but thats later down the road. 在弄清楚如何做到这一点之后,下一步就是根据屏幕尺寸调整layout_width,但是那以后就可以了。 It needs to be done programmatically because a user can theoretically have as many rows as they want to. 这需要以编程方式完成,因为理论上用户可以拥有任意数量的行。

private OnClickListener btnListener = new OnClickListener()
    {

        public void onClick(View v)
        {   

            tablerow = new TableRow(getActivity());

            ed1 = new EditText(getActivity());
            ed1.setInputType(InputType.TYPE_CLASS_TEXT);

            ed2 = new EditText(getActivity());
            ed2.setInputType(InputType.TYPE_NUMBER_FLAG_DECIMAL);

            ed3 = new EditText(getActivity());
            ed3.setInputType(InputType.TYPE_NUMBER_FLAG_DECIMAL);

            tablerow.addView(ed1);
            tablerow.addView(ed2);
            tablerow.addView(ed3);
            table1.addView(tablerow);

        } 

    };  

I believe what you are looking for is LayoutParams and its "weight" value. 我相信您正在寻找的是LayoutParams及其“重量”值。

Try: 尝试:

TableRow.LayoutParams params = new TableRow.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT,1.0f);

ed1 = new EditText(getActivity());
ed1.setInputType(InputType.TYPE_CLASS_TEXT);
ed1.setLayoutParams(params);

The third value (1.0f) in the LayoutParams constructor is a weight...all EditTexts in that TableRow should end up the same width. LayoutParams构造函数中的第三个值(1.0f)是权重...该TableRow中的所有EditTexts最终应具有相同的宽度。

You can try next approach, it is based on xml. 您可以尝试下一种方法,它基于xml。 You will be able to play around with layout at table_row.xml . 您将可以在table_row.xmltable_row.xml布局。

   public class MainActivity extends ActionBarActivity implements View.OnClickListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.a_mn_button_add_row:
                onClickButtonAddRow();
                break;
        }
    }

    private void onClickButtonAddRow() {
        TableLayout tableLayout = (TableLayout) findViewById(R.id.a_mn_table);
        tableLayout.addView(View.inflate(this, R.layout.table_row, null));
    }
}

activity_main.xml activity_main.xml

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/a_mn_button_add_row"
        android:onClick="onClick"
        android:text="Add row"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

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

        <TableLayout
            android:id="@+id/a_mn_table"
            android:stretchColumns="0,1,2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    </ScrollView>

</LinearLayout>

table_row.xml table_row.xml

<?xml version="1.0" encoding="utf-8"?>
<TableRow xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="text" />

    <EditText
        android:inputType="text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <EditText
        android:inputType="text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</TableRow>

table_row.xml for use case with 3-1-2 proportions. table_row.xml用于具有3-1-2比例的用例。

<?xml version="1.0" encoding="utf-8"?>
<TableRow xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <EditText
        android:layout_weight="3"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:inputType="text" />

    <EditText
        android:layout_weight="1"
        android:inputType="text"
        android:layout_width="0dp"
        android:layout_height="wrap_content" />

    <EditText
        android:layout_weight="2"
        android:inputType="text"
        android:layout_width="0dp"
        android:layout_height="wrap_content" />

</TableRow>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Android 数据绑定 layout_width 和 layout_height - Android Data Binding layout_width and layout_height android:textview将自身调整为错误的layout_width - android:textview adjusts itself to wrong layout_width Android自定义组件:找不到layout_width属性 - Android custom component: layout_width attribute not found 如何在Android中修复提供的layout_width属性 - How to fix supply a layout_width attribute in Android 如何根据 Android 布局中的 layout_width 指定 layout_height 值? - How can I specify the layout_height value based on the layout_width in Android layout? 如何从View构造函数设置layout_width / height - How do you set layout_width/height from View constructor 在Android应用中获取消息:二进制XML文件第2行:您必须提供layout_width属性 - Getting message in android app: Binary XML file line #2: You must supply a layout_width attribute 在 package 'android' / new pacakge 中找不到属性 'layout_width' 的资源标识符 - No resource identifier found for attribute 'layout_width' in package 'android' / new pacakge 使用layout_width =“ wrap_content”获取TextView的可用宽度 - Get available width for TextView with layout_width=“wrap_content” Android:如何以编程方式设置表格行背景的宽度 - Android: How to programatically set the width of a tablerow background
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM