Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/229.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/1/ms-access/4.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 setBackgroundColor导致表格布局对齐问题_Android - Fatal编程技术网

Android setBackgroundColor导致表格布局对齐问题

Android setBackgroundColor导致表格布局对齐问题,android,Android,根据以前的请求重新措辞。。。我在运行时动态添加TableLayouts。视图详细信息如下所示。。。LinearLayout->ScrollView->TableLayout->在运行时循环创建TableLayouts和TableRows。因此,基本上我循环并添加以下内容: tableLayoutRow1.setOnTouchListener(new OnTouchListener() { @Override public b

根据以前的请求重新措辞。。。我在运行时动态添加TableLayouts。视图详细信息如下所示。。。LinearLayout->ScrollView->TableLayout->在运行时循环创建TableLayouts和TableRows。因此,基本上我循环并添加以下内容:

            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;
            }
        });
对于(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的对齐方式,并使它们看起来像这样:

左中右

底部

整个代码如下。我希望这一点现在已经清楚了

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);
}
公共类TableActivity扩展活动{
/**在首次创建活动时调用*/
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
LinearLayout LinearLayout=新的LinearLayout(本);
linearLayout.setLayoutParams(新的LayoutParams(LayoutParams.FILL\u父级,LayoutParams.FILL\u父级));
线性布局。设置权重和(1);
ScrollView ScrollView=新的ScrollView(此);
scrollView.setLayoutParams(新的LayoutParams(LayoutParams.FILL\u父级,LayoutParams.FILL\u父级));
TableLayout tableLayoutMaster=新的TableLayout(此);
tableLayoutMaster.setLayoutParams(新的LayoutParams(LayoutParams.FILL\u父级,LayoutParams.FILL\u父级));
// 
//      
//      
// 

对于(int i=0;i我遇到的问题与您在我工作的项目中遇到的问题几乎完全相同。我花了很长时间试图找出问题所在,但最终唯一的解决办法是放弃表格布局,转而做其他事情。在我看来,这像是系统中的一个bug


如果可能的话,我建议改用RelativeLayout。

我在一个活动中测试了您的代码,我不认为也不认为对齐不好,当触摸表行时,它看起来很好…(我的意思是onTouchEvent不会弄乱布局对齐。 无论如何,我建议您使用ListActivity。如果这样做,您可以将选择器背景设置为内部视图,并将颜色或可绘制设置为视图的任何状态、选定、按下……等等。在内部视图中,您可以使用相对布局或其他布局。。。
还有一件事,ListActivity使用非常高效的ListAdapters,视图是虚拟化的,这意味着设备不必保存所有视图,只保存那些可见的视图,而不是您实际保存的30*4视图。

感谢你们两位的深入了解。我将查看ListActivity和ListAdapters,但这更像是一个问题在这一点上,理解远征比任何事情都重要。我使用朱哈尼建议的大锤方法来完成这项工作

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);
}
公共类TestLayout扩展活动{
/**在首次创建活动时调用*/
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
ScrollView ScrollView=新的ScrollView(此);
scrollView.setLayoutParams(新的LayoutParams(LayoutParams.FILL\u父级,LayoutParams.FILL\u父级));
LinearLayout\u master=新的LinearLayout(本);
layout_master.setLayoutParams(新的LayoutParams(LayoutParams.FILL_父级,LayoutParams.FILL_父级));
布局\主设置方向(线性布局。垂直);
scrollView.addView(布局主视图);
对于(int i=1;i