setBackgroundColor导致TableLayout对齐问题

时间:2010-11-24 22:15:32

标签: android

根据先前的请求重新编写代码...我在运行时动态添加TableLayouts。视图纲要看起来像这样...... LinearLayout-> ScrollView-> TableLayout->循环在运行时创建TableLayouts和TableRows。所以基本上我循环并添加以下内容:

for(i = 0 .... i ++)

左中右翼

现在一切都正确添加,看起来正确并正确滚动。但是,我正在尝试创建一个onTouch事件,该事件会更改触摸动态创建的TableLayout上的背景。在每个动态创建的TableLayout的onTouch内部,我有一个onTouch事件,在调用时会执行以下操作:

            tableLayoutRow1.setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                Log.d("OnTouch", "OnTouch Event for " + Integer.toString(v.getId()) + " event:" + Integer.toString(event.getAction()));

                switch(event.getAction())
                {
                    case MotionEvent.ACTION_DOWN:
                        Log.d("OnTouch", "Changing to GREEN");
                        v.setBackgroundColor(Color.GRAY);
                        break;
                    case MotionEvent.ACTION_UP:
                    case MotionEvent.ACTION_MOVE:
                        Log.d("OnTouch", "Changing to BLACK");
                        v.setBackgroundColor(Color.BLACK);
                        break;
                }

                return true;
            }
        });

每当它调用setBackgroundColor时,它会混乱每个动态创建的TableLayouts的对齐方式,并使它们看起来像这样:

LeftMiddleRight

整个代码如下。我希望现在很清楚。

public class TableActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    LinearLayout linearLayout = new LinearLayout(this);
    linearLayout.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
    linearLayout.setWeightSum(1);

    ScrollView scrollView = new ScrollView(this);
    scrollView.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));

    TableLayout tableLayoutMaster = new TableLayout(this);
    tableLayoutMaster.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));

    // <table>
    //      <tr><td><table><tr><td></td><td></td><td></td></tr></table></td></tr>
    //      <tr><td></td></tr>
    // </table>

    for(int i=0; i<=30; i++)
    {
        TableLayout tableLayoutRow1 = new TableLayout(this);
        tableLayoutRow1.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT)); 
        tableLayoutRow1.setColumnStretchable(2, true);
        //tableLayoutRow1.setBackgroundColor(Color.GREEN);
        TableRow tableRow = new TableRow(this);

        ImageView left_column = new ImageView(this);
        left_column.setImageResource(17301550);
        tableRow.addView(left_column);

        TextView middle_column = new TextView(this);
        middle_column.setText("Left Column:" + Integer.toString(i));
        middle_column.setTypeface(null, Typeface.BOLD);
        middle_column.setGravity(Gravity.LEFT);
        middle_column.setPadding(5, 0, 0, 0);
        tableRow.addView(middle_column);

        TextView right_column = new TextView(this);
        right_column.setText("Right Column:" + Integer.toString(i));
        right_column.setTypeface(null, Typeface.BOLD);
        right_column.setGravity(Gravity.RIGHT);
        right_column.setPadding(0, 0, 5, 0);
        tableRow.addView(right_column);

        tableLayoutRow1.addView(tableRow);
        tableRow = null;

        // now for the bottom row
        TextView bottom_column = new TextView(this);
        bottom_column.setText("Starting activity com.developersinfo.testing.TableActivity on device emulator-5554");
        bottom_column.setTextSize(12f);
        bottom_column.setGravity(Gravity.LEFT);
        bottom_column.setPadding(30, 0, 0, 0);
        tableLayoutRow1.addView(bottom_column);

        tableLayoutRow1.setId(i);
        tableLayoutRow1.setOnFocusChangeListener(new OnFocusChangeListener() {

            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                // TODO Auto-generated method stub
                Log.d("OnFocusChange", "OnFocusChange Event for " + Integer.toString(v.getId()));
            }
        });
        tableLayoutRow1.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Log.d("OnClick", "OnClick Event for " + Integer.toString(v.getId()));
            }
        });
        tableLayoutRow1.setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                Log.d("OnTouch", "OnTouch Event for " + Integer.toString(v.getId()) + " event:" + Integer.toString(event.getAction()));

                switch(event.getAction())
                {
                    case MotionEvent.ACTION_DOWN:
                        Log.d("OnTouch", "Changing to GREEN");
                        v.setBackgroundColor(Color.GRAY);
                        break;
                    case MotionEvent.ACTION_UP:
                    case MotionEvent.ACTION_MOVE:
                        Log.d("OnTouch", "Changing to BLACK");
                        v.setBackgroundColor(Color.BLACK);
                        break;
                }

                return true;
            }
        });

        // now add to master table
        tableLayoutMaster.addView(tableLayoutRow1);
    }

    scrollView.addView(tableLayoutMaster);
    linearLayout.addView(scrollView);
    setContentView(linearLayout);
    this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
}

}

我已经尝试了所有我能想到的东西,以防止它这样做,但到目前为止,我无法解释为什么或如何纠正它。只要我不在onTouch中调用setBackgroundColor,就会按预期滚动和对齐。

3 个答案:

答案 0 :(得分:0)

我在我工作的项目中遇到的问题几乎完全相同。我花了很长时间试图弄清楚什么是错的,但最后只有解决方案就是抛弃桌面布局而去寻找别的东西。对我来说,它看起来像系统中的一个错误。

如果可能,我建议使用RelativeLayout。

答案 1 :(得分:0)

我在一个活动中测试你的代码,我不认为并且没有看到对齐是坏的,当触摸表格行时它看起来不错......(我的意思是onTouchEvent不会弄乱布局对齐。 无论如何,我建议你使用ListActivity,如果这样做,你可以设置一个选择器背景到内部视图,一个颜色或drawable到任何视图状态,选择,按下......以及更多...和内部视图内部你可以使用相对布局或其他布局...... 还有一件事,ListActivity使用非常有效的ListAdapter,视图被虚拟化意味着设备不必保存所有视图,只有那些可见的视图而不是实际持有的30 * 4视图。

答案 2 :(得分:0)

感谢你们两位的见解。我将查看ListActivity和ListAdapters,但这更像是一次了解远征而不是此时的任何事情。我有这个工作使用Juhani建议的大锤方法。

public class TestLayout extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    ScrollView scrollView = new ScrollView(this);
    scrollView.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));

    LinearLayout layout_master = new LinearLayout(this);
    layout_master.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
    layout_master.setOrientation(LinearLayout.VERTICAL);

    scrollView.addView(layout_master);

    for(int i=1; i<=30; i++)
    {
        RelativeLayout layout_body = new RelativeLayout(this);

        TextView left_Column = new TextView(this);
        left_Column.setId(i);
        left_Column.setText("Left");
        left_Column.setGravity(Gravity.LEFT);
        left_Column.setPadding(5, 0, 5, 0);

        TextView middle_Column = new TextView(this);
        RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        lp.addRule(RelativeLayout.RIGHT_OF, left_Column.getId());
        middle_Column.setLayoutParams(lp);
        middle_Column.setText("Middle");
        middle_Column.setGravity(Gravity.LEFT);
        middle_Column.setPadding(5, 0, 5, 0);

        TextView right_Column = new TextView(this);
        lp = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        lp.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
        right_Column.setLayoutParams(lp);
        right_Column.setText("Right");
        right_Column.setGravity(Gravity.RIGHT);
        right_Column.setPadding(5, 0, 5, 0);

        layout_body.addView(left_Column);
        layout_body.addView(middle_Column);
        layout_body.addView(right_Column);

        TextView bottom = new TextView(this);
        bottom.setId(1000 + i);
        lp = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        lp.addRule(RelativeLayout.BELOW, left_Column.getId());
        bottom.setLayoutParams(lp);
        bottom.setText("Starting activity com.developersinfo.testing.TestLayout on device emulator-5554");
        layout_body.addView(bottom);

        View view_hr = new View(this);
        view_hr.setMinimumWidth(layout_body.getWidth());
        view_hr.setMinimumHeight(1);
        view_hr.setBackgroundColor(Color.GRAY);
        lp = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        lp.addRule(RelativeLayout.BELOW, bottom.getId());
        view_hr.setLayoutParams(lp);
        layout_body.addView(view_hr);

        layout_body.setOnTouchListener(new OnTouchListener() {

            public boolean onTouch(View v, MotionEvent event) {
                switch(event.getAction())
                {
                    case MotionEvent.ACTION_DOWN:
                        Log.d("OnTouch", "Changing to GREEN");
                        v.setBackgroundColor(Color.GREEN);
                        break;
                    default:
                        Log.d("OnTouch", "Changing to BLACK");
                        v.setBackgroundColor(Color.BLACK);
                        break;
                }
                return true;
            }
        });

        layout_master.addView(layout_body);
    }

    //scrollView.addView(layout_master);
    setContentView(scrollView);
}

}