Adapt view for screens with lower resolution

牧云@^-^@ 提交于 2019-12-25 17:03:09

问题


I tested my app, that I developed over the past two months always on a smartphone with 5 inches. Now, I have big problem with showing all the content of my view.

First, here´s my xml:

<FrameLayout  xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" 


    android:background="@drawable/bg">



<TableLayout

    android:layout_width="match_parent"
    android:layout_height="match_parent">


    <TableRow
        android:id="@+id/tableRow1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

        <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/willi"
        />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="70dp"


        android:background="@drawable/willi"
         />

    <Button
        android:id="@+id/button3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
    android:layout_marginLeft="140dp"
        android:background="@drawable/willi"
/>

    <Button
        android:id="@+id/button4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
    android:layout_marginLeft="210dp"

        android:background="@drawable/willi"
         />


    </TableRow>


    <TableRow
        android:id="@+id/tableRow2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" 
        android:layout_marginTop="90dp">



         <Button
        android:id="@+id/button5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"

        android:background="@drawable/willi"
        />

    <Button
        android:id="@+id/button6"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="70dp"
        android:background="@drawable/willi"
   />

    <Button
        android:id="@+id/button7"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="140dp"
        android:background="@drawable/willi"
         />

    <Button
        android:id="@+id/button8"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="210dp"
        android:background="@drawable/willi"
         />

        </TableRow>
       <TableRow
        android:id="@+id/tableRow3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="90dp" >


       <Button
        android:id="@+id/button9"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"

        android:background="@drawable/willi" />
    <Button

        android:id="@+id/button10"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="70dp"
        android:background="@drawable/willi"  />

    <Button
        android:id="@+id/button11"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="140dp"
        android:background="@drawable/willi"  />

    <Button
        android:id="@+id/button12"
        android:layout_marginLeft="210dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"

        android:background="@drawable/willi" />

    </TableRow>



              <TableRow
        android:id="@+id/tableRow4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="90dp" >


       <Button
        android:id="@+id/button13"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"

        android:background="@drawable/willi" />
    <Button

        android:id="@+id/button14"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="70dp"
        android:background="@drawable/willi"  />

    <Button
        android:id="@+id/button15"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="140dp"
        android:background="@drawable/willi"  />

    <Button
        android:id="@+id/button16"
        android:layout_marginLeft="210dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"

        android:background="@drawable/willi" />

    </TableRow>

      <TableRow
        android:id="@+id/tableRow5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="90dp" >


       <Button
        android:id="@+id/button17"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"

        android:background="@drawable/willi" />
    <Button

        android:id="@+id/button18"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="70dp"
        android:background="@drawable/willi"  />

    <Button
        android:id="@+id/button19"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="140dp"
        android:background="@drawable/willi"  />

    <Button
        android:id="@+id/button20"
        android:layout_marginLeft="210dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"

        android:background="@drawable/willi" />

    </TableRow>

    <TableRow
        android:id="@+id/tableRow6"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="90dp" >


       <Button
        android:id="@+id/button21"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"

        android:background="@drawable/willi" />
    <Button

        android:id="@+id/button22"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="70dp"
        android:background="@drawable/willi"  />

    <Button
        android:id="@+id/button23"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="140dp"
        android:background="@drawable/willi"  />

    <Button
        android:id="@+id/button24"
        android:layout_marginLeft="210dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"

        android:background="@drawable/willi" />


    </TableRow>

    </TableLayout>

<TextView
        android:id="@+id/tvPunktewaehrend"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:textStyle="bold"
        android:layout_gravity="center_horizontal"
        android:textColor="@color/white"
        android:textSize="20sp"
        android:text=" " />


</FrameLayout>

Like you see, I have 6 table rows with 4 buttons in each. On the HTC Wildfire I see the fifth row very small and the sixth one is out of my view. How can I solve this problem for small screens?


回答1:


Change the TableLayout for vertical LinearLayout, give it a weightSum. Then add horizontal LinearLayouts to it having Buttons as child. For parent LinearLayout use weightSum and for child LinearLayout use weight.

So for 6 rows, you have weightSum 6 and each LinearLayout height 0dp and weight 1.




回答2:


What you can do is use a ScrollView as your Primary Layout, and will not need to modify the size of your buttons and everything, just like you can move the scroll bar, I used it I think applications for different screen sizes. I hope you serve something.




回答3:


I see the fifth row very small and the sixth one is out of my view. How can I solve this problem for small screens?

So you can use dimens.xml file in which you can put your attributes like padding/margins according to your requirement by seeing your layout in different resolutions.

The Contents of dimens.xml can be like:

<resources>

    <!-- Default screen margins, per the Android Design guidelines. -->
    <dimen name="activity_horizontal_margin">16dp</dimen>
    <dimen name="activity_vertical_margin">16dp</dimen>

</resources>

According to your screen size/resolution you have to put this file as:

res/values/dimens.xml

res/values-hdpi/dimens.xml

res/values-mdpi/dimens.xml

res/values-sw360dp-hdpi/dimens.xml

res/values-sw360dp-long-xhdpi/dimens.xml

res/values-sw600dp/dimens.xml

res/values-sw720dp-land/dimens.xml

To use this value: For e.g. Your Table Row 1 > Button 2 : Put

android:layout_marginLeft="@dimen/activity_horizontal_margin"

instead of

android:layout_marginLeft="70dp"


来源:https://stackoverflow.com/questions/21634679/adapt-view-for-screens-with-lower-resolution

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!