Android:设置 View 的 setVisibility(visible) 时 TableLayout 发生变化

标签 android visibility tablelayout tablerow

大家好,我在使用我正在开发的应用程序的屏幕时遇到了一些问题。当我单击“确定”按钮时,ImageButton (btnSelect) 应该设置可见性 (GONE) 并且 ImageView (OkIcon) 应该出现,但这会缩小 EditText 的空间,如您在这些屏幕截图中所见。

http://imageshack.us/photo/my-images/17/problem1ei.png/

http://imageshack.us/photo/my-images/852/problem2m.png/

为什么会这样?

代码:

@Override
public void onClick(View v) {
    InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        if (v.getId() == R.id.init_select1) {
            if (validateSal(this.edtTextInit1.getText().toString())) {
                saveSal(this.edtTextInit1.getText().toString());
                imm.hideSoftInputFromWindow(edtTextInit1.getWindowToken(),0);
                edtTextInit1.setEnabled(false);
                btnSelect1.setVisibility(View.GONE);
                okIcon1.setVisibility(View.VISIBLE);
                btnSelect2.setVisibility(View.VISIBLE);
            }

布局的 XML:

<ScrollView
    android:layout_height="fill_parent"
    android:layout_width="fill_parent">
    <TableLayout
        android:gravity="center"
        android:layout_height="fill_parent"
        android:layout_width="fill_parent"
        android:layout_weight="1"
        android:shrinkColumns="0">
        <TableRow>
            <TextView
                style="@style/styleTextLabel"
                android:layout_height="wrap_content"
                android:layout_width="wrap_content"
                android:text="@string/label_init"
                android:id="@+id/label_init"
                android:layout_margin="10dip">
            </TextView>
        </TableRow>

        <TableRow>
            <EditText
                android:layout_column="0"
                android:layout_marginRight="10dip"
                android:layout_marginLeft="10dip"
                android:layout_marginBottom="10dip"
                style="@style/styleInputInit"
                android:id="@+id/input_init1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content">
            </EditText>
            <ImageView
                android:visibility="gone"
                android:layout_marginRight="10dip"
                android:layout_marginLeft="10dip"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/okicon"
                android:id="@+id/init_ok1">
            </ImageView>
            <ImageButton
                android:layout_column="1"
                android:layout_marginRight="10dip"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/init_select1"
                android:background="@drawable/btn_ok_">
            </ImageButton>
        </TableRow>

        <TableRow>
            <EditText
                android:layout_column="0"
                android:layout_marginRight="10dip"
                android:layout_marginLeft="10dip"
                android:layout_marginBottom="10dip"
                android:id="@+id/input_init2"
                style="@style/styleInputInit"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content">
            </EditText>
            <ImageView
                android:layout_column="1"
                android:visibility="gone"
                android:layout_marginRight="10dip"
                android:layout_marginLeft="10dip"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/okicon"
                android:id="@+id/init_ok2">>
            </ImageView>
            <ImageButton
                android:layout_column="1"
                android:layout_marginRight="10dip"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/init_select2"
                android:background="@drawable/btn_ok_"
                android:visibility="gone">
            </ImageButton>
        </TableRow> (...)

最佳答案

尝试像这样给 EditText 增加权重:

    <TableRow>
        <EditText
            android:layout_column="0"
            android:layout_marginRight="10dip"
            android:layout_marginLeft="10dip"
            android:layout_marginBottom="10dip"
            style="@style/styleInputInit"
            android:id="@+id/input_init1"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content">
        </EditText>
        <ImageView
            android:visibility="gone"
            android:layout_marginRight="10dip"
            android:layout_marginLeft="10dip"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/okicon"
            android:id="@+id/init_ok1">
        </ImageView>
        <ImageButton
            android:layout_column="1"
            android:layout_marginRight="10dip"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/init_select1"
            android:background="@drawable/btn_ok_">
        </ImageButton>
    </TableRow>

    <TableRow>
        <EditText
            android:layout_column="0"
            android:layout_marginRight="10dip"
            android:layout_marginLeft="10dip"
            android:layout_marginBottom="10dip"
            android:id="@+id/input_init2"
            style="@style/styleInputInit"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content">
        </EditText>
        <ImageView
            android:layout_column="1"
            android:visibility="gone"
            android:layout_marginRight="10dip"
            android:layout_marginLeft="10dip"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/okicon"
            android:id="@+id/init_ok2">>
        </ImageView>
        <ImageButton
            android:layout_column="1"
            android:layout_marginRight="10dip"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/init_select2"
            android:background="@drawable/btn_ok_"
            android:visibility="gone">
        </ImageButton>
    </TableRow> (...)

关于Android:设置 View 的 setVisibility(visible) 时 TableLayout 发生变化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6100702/

相关文章:

android - 如何在线程内动态创建表格布局

Android:隐藏一个元素

c# - bool 到 System.Windows.Visibility(动态创建 DataGridColumnHeaders 时的绑定(bind)问题)

android - 房间 : replace SQLite cursor in Dao?

java - 要求输出始终采用小数点右侧 2 位、左侧 7 位格式

asp.net - 在 ASP.NET 中使用链接和 anchor 切换可见性的最佳方法是什么?

Android:ListView性能还是不够怎么办?

android - 如何在代码中设置 View 的布局列( View 在表格行内)?

Android:在 res 中使用 SVG 会导致错误: "The file name must end with .xml or .png"

android - Android 中的 SD 卡文件浏览器