如何解决不同屏幕尺寸的问题?

时间:2016-07-25 14:06:51

标签: android android-layout

我在设备上展示了界面。

我在向模拟器演示时发现了设备大小及其大小的差异。

我该怎么办?尺码适合大多数设备?看看两张图片之间的区别:

screenshot 1screenshot 2

package com.examp.swap_items;
import java.util.ArrayList;
import java.util.List;

import android.R.layout;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.HorizontalScrollView;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.ContextThemeWrapper;
import android.view.Gravity;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TableRow.LayoutParams;
import android.widget.TextView;
import android.app.Activity;

public class MainActivity extends Activity {
    private boolean showSummaries;

    private TableLayout summaryTable;
    private TableLayout frozenTable;
    private TableLayout contentTable;
    private Button backButton ;
    private HorizontalScrollView hor;
    private TextView recyclableTextView;

    private String[] allColors ;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
           setContentView(R.layout.activity_main);


           Color[] colors = {}; 


           TableRow.LayoutParams wrapWrapTableRowParams = new TableRow.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
            int[] fixedColumnWidths = new int[]{20,20, 20,20, 20, 20};


            int[] scrollableColumnWidths = new int[]{20,20, 20, 20, 20, 20};
            int fixedRowHeight = 50;
            int fixedHeaderHeight = 60;
            TableRow row = new TableRow(this);
            //header (fixed vertically)
            TableLayout header=(TableLayout)findViewById(R.id.table_header);
            row.setLayoutParams(wrapWrapTableRowParams);
            row.setGravity(Gravity.CENTER);
            row.setBackgroundColor(Color.WHITE);
            row.addView(makeTableRowWithText("col1",fixedColumnWidths[0],fixedHeaderHeight));
            row.addView(makeTableRowWithText("col2",fixedColumnWidths[1],fixedHeaderHeight));
            row.addView(makeTableRowWithText("col3",fixedColumnWidths[2],fixedHeaderHeight));
            row.addView(makeTableRowWithText("col4",fixedColumnWidths[3],fixedHeaderHeight));
            row.addView(makeTableRowWithText("col5",fixedColumnWidths[4],fixedHeaderHeight));
           // row.addView(makeTableRowWithText("col6", fixedColumnWidths[5],fixedHeaderHeight));
            header.addView(row);


            hor = (HorizontalScrollView)findViewById(R.id.scroller);
            hor.postDelayed(new Runnable(){
                public void run() {
                    hor.fullScroll(HorizontalScrollView.FOCUS_RIGHT);
                }},1L);


           TableLayout fixedColumn = (TableLayout) findViewById(R.id.scrollable_part2);
            //rest of the table (within a scroll view)
            TableLayout scrollablePart = (TableLayout)findViewById(R.id.scrollable_part);
            for(int i = 0; i < 10; i++) {
                TextView fixedView = makeTableRowWithText("fixed number " + i, scrollableColumnWidths[0], fixedRowHeight);
                fixedView.setBackgroundColor(Color.GREEN);
                fixedView.setGravity(Gravity.RIGHT);
                fixedColumn.addView(fixedView);
                row = new TableRow(this);
                row.setLayoutParams(wrapWrapTableRowParams);
                row.setGravity(Gravity.LEFT);
                if(i==1)row.setBackgroundColor(Color.RED);
                if(i==2)row.setBackgroundColor(Color.MAGENTA);
                if(i==3)row.setBackgroundColor(Color.YELLOW);
                if(i==4)row.setBackgroundColor(Color.GRAY);
                if(i==5)row.setBackgroundColor(Color.LTGRAY);
                if(i==6)row.setBackgroundColor(Color.WHITE);
                if(i==7)row.setBackgroundColor(Color.CYAN);
                if(i==8)row.setBackgroundColor(Color.TRANSPARENT);
                if(i==9)row.setBackgroundColor(Color.MAGENTA);
                row.addView(makeTableRowWithText("scroll 1", scrollableColumnWidths[1], fixedRowHeight));
                row.addView(makeTableRowWithText("scroll 2", scrollableColumnWidths[1], fixedRowHeight));
                row.addView(makeTableRowWithText("scroll 3", scrollableColumnWidths[2], fixedRowHeight));
                row.addView(makeTableRowWithText("scroll 4", scrollableColumnWidths[3], fixedRowHeight));
                row.addView(makeTableRowWithText("scroll 5", scrollableColumnWidths[4], fixedRowHeight));
                row.addView(makeTableRowWithText("scroll 6", scrollableColumnWidths[5], fixedRowHeight));
                scrollablePart.addView(row);
            }
    }
     public TextView makeTableRowWithText(String text, int widthInPercentOfScreenWidth, int fixedHeightInPixels) {
            int screenWidth = getResources().getDisplayMetrics().widthPixels;
            recyclableTextView = new TextView(this);
            recyclableTextView.setText(text);
            recyclableTextView.setTextColor(Color.BLACK);
            recyclableTextView.setTextSize(20);
            recyclableTextView.setWidth(widthInPercentOfScreenWidth * screenWidth / 100);
            recyclableTextView.setHeight(fixedHeightInPixels);
            return recyclableTextView;
        }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

和XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:gravity="right"
    android:id="@+id/fillable_area">
    <TableLayout
      android:gravity="right"
      android:id="@+id/table_header"
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"/>
    <ScrollView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">

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

         <HorizontalScrollView
         android:id="@+id/scroller"
         android:layout_width="0dp"
         android:layout_weight="1"
         android:layout_height="wrap_content">

             <TableLayout
                android:id="@+id/scrollable_part"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"/>
         </HorizontalScrollView>

         <TableLayout
                android:id="@+id/scrollable_part2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"/>
    </LinearLayout>

    </ScrollView>

</LinearLayout>

1 个答案:

答案 0 :(得分:0)

你可以分配重量而不是重力吗?我不确定。也许你可以做match_parent而不是fill_parent?我不确定,我仍然是一个制作应用程序和东西的菜鸟。请投票。

相关问题