调整gridview以适应不同的屏幕尺寸

时间:2016-08-29 17:47:19

标签: android android-layout gridview

我们有一个包含3列和11行的gridview,其中包含textview,gridview嵌套在relativelayout中。当我在Nexus 5X 5.2上渲染活动完美时。在Nexus S 4in屏幕上渲染gridview时,拟合不会调整大小。所以我的问题是我需要一个Fragment只用于GridView。我尝试使用res / layout activity_page.xml作为master和activity_two.xml sw240dp,并在sw240dp中复制并粘贴主布局,并更改textview中文本的大小和下面行示例代码的宽度。在这一点上,我不确定这是使用gridview渲染文本的最佳方式,还是我需要片段而不是我不知道如何编写片段。 Android看到两个仿真器的大小相同,所以我需要了解很多有关屏幕尺寸和布局的信息。

    <GridLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_below="@+id/btnCalc"
    android:columnCount="3"
    android:rowCount="11"
    android:layout_marginTop="20dp"
    android:background="#f9f4f4"
    android:useDefaultMargins="false">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Category"
        android:id="@+id/textView6"
        android:width="200dp"
        android:textSize="20sp"
        android:textStyle="bold"
        android:layout_row="0"
        android:layout_column="0"
        android:background="#6aa6f5"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="BMI range kg/m2"
        android:id="@+id/textView7"
        android:layout_row="0"
        android:layout_column="2"
        android:textSize="20sp"
        android:textStyle="bold"
        android:width="180dp"
        android:background="#6aa6f5"
        />

1 个答案:

答案 0 :(得分:1)

@James_Duh这里有一些代码可以使用,但我觉得这不是处理你问题的最优雅方式。我担心的是这种方法使用的处理能力和时间有多少开销。还有什么对记忆的影响?我没有能力回答这些问题,也许其他用户会扩展这些问题。我想知道如何通过更改GridView的属性或使用样式来做到这一点。我的解决方案有可能为更大的项目创建大量代码。这就是说这里列出的代码是为了您的乐趣 `public class PageThree扩展了AppCompatActivity {

Button button;
TextView TVinfo;
TextView TVone, TVtwo;
RelativeLayout RLpage3;
TextView tv1,tv2,tv3,tv4,tv5,tv6;
//GridView GVtext;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate( savedInstanceState );
    setContentView( R.layout.activity_page_three );

    button = (Button) findViewById( R.id.button );
    TVinfo = (TextView) findViewById( R.id.TVinfo );
    TVone = (TextView) findViewById( R.id.TVone );
    TVtwo = (TextView) findViewById( R.id.TVtwo );
    RLpage3 = (RelativeLayout) findViewById( R.id.RLpage3 );
    tv1 = (TextView)findViewById( R.id.tv1 );
    tv2 = (TextView)findViewById( R.id.tv2 );
    tv3 = (TextView)findViewById( R.id.tv3 );
    tv4 = (TextView)findViewById( R.id.tv4 );
    tv5 = (TextView)findViewById( R.id.tv5 );
    tv6 = (TextView)findViewById( R.id.tv6 );
    //GVtext = (GridView)findViewById( R.id.GVtext );


    DisplayMetrics metrics = getResources().getDisplayMetrics();
    int DeviceTotalWidth = metrics.widthPixels;
    int dens = metrics.densityDpi;
    int dp = (DeviceTotalWidth * 160/dens);
    TVtwo.setText( String.valueOf( DeviceTotalWidth +" "+dens +" "+ dp) );
    int dpL = dp ;
    int dpR = dp ;
    int ts = 0;
    String L = String.valueOf( dpL+"sp" );
    TVone.setText( L );

    if (DeviceTotalWidth == 480) {
        if (dp == 320) {
            dpL = 240;
            dpR = 200;
            ts = 14;
        }
    }

    if (DeviceTotalWidth == 768) {
        if (dp == 384) {
            dpL = 360;
            dpR = 360;
            ts = 20;
        }
    }
    if (DeviceTotalWidth == 1080) {
        if (dp == 411) {
            dpL = 520;
            dpR = 550;
            ts = 20;
        }
    }
    if (DeviceTotalWidth == 1536) {
        if (dp == 768) {
            dpL = 740;
            dpR = 790;
            ts = 20;
        }
    }
            TVone.setTextSize( 16 );
            tv1.setTextSize( ts );
            tv2.setTextSize( ts );
            tv3.setTextSize( ts );
            tv4.setTextSize( ts );
            tv5.setTextSize( ts );
            tv6.setTextSize( ts );
            tv1.setWidth( dpL );
            tv2.setWidth( dpR );
            tv3.setWidth( dpL );
            tv4.setWidth( dpR );
            tv5.setWidth( dpL );
            tv6.setWidth( dpR );

        }`
相关问题