0

我在 Android 中有一个带有 RelativeLayout 的屏幕,有一个 TableLayout 和一个 TableRow,然后是 6 个小部件:4 个 TextView 和 2 个按钮。

当我在平板电脑大小的模拟器上显示它时,所有小部件都在里面。但是当我移动到手机大小的模拟器(5 英寸)时,两个按钮中的一个半消失在右侧。

我希望RelativeLayout 会在屏幕上显示所有这些。我怎样才能做到这一点?

我的 RelativeLayout 声明如下:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:paddingLeft="16dp"
  android:paddingRight="16dp" >

表格布局:

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@+id/cancelTable"
  android:layout_below="@+id/pageheader"
  android:layout_width="fill_parent"
android:layout_height="fill_parent" >

表行:

<TableRow
    android:id="@+id/tableRow1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:padding="5dip" >

TextView 和 Button 在 Activity 的 onCreate 中启动:

    table = (TableLayout) findViewById(R.id.cancelTable);
    table.removeAllViewsInLayout();

    TableRow tr=new TableRow(Edit.this);
    tr.removeAllViewsInLayout();
    int flag = 1;       
    if(flag == 1) {
        TextView b11=new TextView(Edit.this);
        b11.setText("Name");
        b11.setTextColor(Color.RED);
        b11.setTextSize(15);
        tr.addView(b11);

        TextView b4 = new TextView(Edit.this);
        b4.setPadding(10, 0, 0, 0);
        b4.setTextSize(15);
        b4.setText("Text");
        b4.setTextColor(Color.RED);
        tr.addView(b4);

        TextView b5=new TextView(Edit.this);
        b5.setPadding(10, 0, 0, 0);
        b5.setText("Number");
        b5.setTextColor(Color.RED);
        b5.setTextSize(15);
        tr.addView(b5);

        TextView b7=new TextView(Edit.this);
        b7.setPadding(10, 0, 0, 0);
        b7.setText("Time");
        b7.setTextColor(Color.RED);
        b7.setTextSize(15);
        tr.addView(b7);

        TextView b8=new TextView(Edit.this);
        b8.setPadding(10, 0, 0, 0);
        b8.setText("Delete");
        b8.setTextColor(Color.RED);
        b8.setTextSize(15);
        tr.addView(b8);

        TextView b9=new TextView(Edit.this);
        b9.setPadding(10, 0, 0, 0);
        b9.setText("Edit");
        b9.setTextColor(Color.RED);
        b9.setTextSize(15);
        tr.addView(b9);


        table.addView(tr);

        final View vline = new View(Edit.this);
        vline.setLayoutParams(new       
        TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, 2));
        vline.setBackgroundColor(Color.RED);
        table.addView(vline); 
        flag=0;
   }

    com.eyal.sms.db.SMS sms;        
    for(int i=0; i<allSched.size(); i++) {

        tr = new TableRow(Edit.this);
        tr.removeAllViewsInLayout();

        sms = allSched.get(i);

        TextView b = new TextView(Edit.this);
        String str = String.valueOf(sms.getToContact());
        b.setText(str != null ? str : "");
        b.setTextColor(Color.WHITE);
        b.setTextSize(15);
        tr.addView(b);

        TextView b1=new TextView(Edit.this);
        b1.setPadding(10, 0, 0, 0);
        b1.setTextSize(15);
        String str1 = sms.getText();
        if (str1.length() > 20) 
            str1 = str1.substring(0, 20); 

        b1.setText(str1);
        b1.setTextColor(Color.WHITE);
        tr.addView(b1);

        TextView b2 = new TextView(Edit.this);
        b2.setPadding(10, 0, 0, 0);
        String str2 = String.valueOf(sms.getToNumber());
        b2.setText(str2);
        b2.setTextColor(Color.WHITE);
        b2.setTextSize(15);
        tr.addView(b2);

        TextView b3 = new TextView(Edit.this);
        b3.setPadding(10, 0, 0, 0);
        String str3 = String.valueOf(sms.getTime());
        b3.setText(str3);
        b3.setTextColor(Color.WHITE);
        b3.setTextSize(15);
        tr.addView(b3);


        Button delete = new Button(Edit.this);
        delete.setText("Delete");            
        DeleteButtonListener dbl = new DeleteButtonListener();
        dbl.setSMS(sms);
        delete.setOnClickListener(dbl);            
        tr.addView(delete);

        Button edit = new Button(Edit.this);
        edit.setText("Edit");
        EditButtonListener ebl = new EditButtonListener();
        ebl.setSMS(sms);
        edit.setOnClickListener(ebl);
        tr.addView(edit);

        table.addView(tr);


        final View vline1 = new View(Edit.this);
        vline1.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, 1));
        vline1.setBackgroundColor(Color.WHITE);
        table.addView(vline1);  // add line below each row  

    }

我错过了什么?RelativeLayout —— 或 Android 中的任何其他布局 —— 能否保证我的所有小部件都在屏幕上,或者至少可以滚动,在所有尺寸的硬件中?

4

1 回答 1

1

试试这个,它会包装你的 TableLayout,并解决你的问题,我希望

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:paddingLeft="16dp"
    android:paddingRight="16dp" >

    <HorizontalScrollView
        android:id="@+id/horizontalScrollView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="horizontal" >

            <TableLayout
                xmlns:android="http://schemas.android.com/apk/res/android"
                android:id="@+id/cancelTable"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:layout_below="@+id/pageheader" >

            </TableLayout>
        </LinearLayout>
    </HorizontalScrollView>

</RelativeLayout>

[编辑 1]

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.widget.Button;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;

public class MainActivity extends Activity {
    TableLayout table;
    TableRow tr;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        table = (TableLayout) findViewById(R.id.cancelTable);
        table.removeAllViewsInLayout();
        tr = new TableRow(MainActivity.this);
        int flag = 1;       
        if(flag == 1) {
            TextView b11=new TextView(MainActivity.this);
            b11.setText("Name");
            b11.setTextColor(Color.RED);
            b11.setTextSize(15);
            tr.addView(b11);

            TextView b4 = new TextView(MainActivity.this);
            b4.setPadding(10, 0, 0, 0);
            b4.setTextSize(15);
            b4.setText("Text");
            b4.setTextColor(Color.RED);
            tr.addView(b4);

            TextView b5=new TextView(MainActivity.this);
            b5.setPadding(10, 0, 0, 0);
            b5.setText("Number");
            b5.setTextColor(Color.RED);
            b5.setTextSize(15);
            tr.addView(b5);

            TextView b7=new TextView(MainActivity.this);
            b7.setPadding(10, 0, 0, 0);
            b7.setText("Time");
            b7.setTextColor(Color.RED);
            b7.setTextSize(15);
            tr.addView(b7);

            TextView b8=new TextView(MainActivity.this);
            b8.setPadding(10, 0, 0, 0);
            b8.setText("Delete");
            b8.setTextColor(Color.RED);
            b8.setTextSize(15);
            tr.addView(b8);

            TextView b9=new TextView(MainActivity.this);
            b9.setPadding(10, 0, 0, 0);
            b9.setText("Edit");
            b9.setTextColor(Color.RED);
            b9.setTextSize(15);
            tr.addView(b9);


            table.addView(tr);

            flag=0;
       }

        for(int i=0; i<30; i++) {
            tr = new TableRow(MainActivity.this);

            TextView b = new TextView(MainActivity.this);
            b.setText("TExt");
            b.setTextColor(Color.WHITE);
            b.setTextSize(15);
            tr.addView(b);

            TextView b1=new TextView(MainActivity.this);
            b1.setPadding(10, 0, 0, 0);
            b1.setTextSize(15);
            b1.setText("text2");
            b1.setTextColor(Color.WHITE);
            tr.addView(b1);

            TextView b2 = new TextView(MainActivity.this);
            b2.setPadding(10, 0, 0, 0);
            b2.setText("text3");
            b2.setTextColor(Color.WHITE);
            b2.setTextSize(15);
            tr.addView(b2);

            TextView b3 = new TextView(MainActivity.this);
            b3.setPadding(10, 0, 0, 0);
            b3.setText("text4");
            b3.setTextColor(Color.WHITE);
            b3.setTextSize(15);
            tr.addView(b3);


            Button delete = new Button(MainActivity.this);
            delete.setText("Delete");            
            tr.addView(delete);

            Button MainActivity = new Button(MainActivity.this);
            MainActivity.setText("MainActivity");            
            tr.addView(MainActivity);

            table.addView(tr);

        }

    }  
}
于 2012-12-06T14:55:36.620 回答