RelativeLayout不显示所有小部件

时间:2012-12-06 14:06:22

标签: android android-layout relativelayout

我在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:

<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:

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

TextView和Buttons在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中的任何其他布局 - 可以保证我的所有小部件都可以在所有尺寸的硬件中显示在屏幕上,或者至少可以滚动吗?

1 个答案:

答案 0 :(得分: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);

        }

    }  
}