当前位置: 动力学知识库 > 问答 > 编程问答 >

java - Android Studio - XML image buttons and text won't fit the size

问题描述:

So, I am creating a workout app and getting problems with imageButtons and text.

Screen-1

content_app_layout_main.xml

<?xml version="1.0" encoding="utf-8"?>

<ScrollView android:layout_width="match_parent"

android:layout_height="wrap_content"

xmlns:android="http://schemas.android.com/apk/res/android" >

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

xmlns:app="http://schemas.android.com/apk/res-auto"

xmlns:tools="http://schemas.android.com/tools"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:paddingBottom="@dimen/activity_vertical_margin"

android:paddingLeft="@dimen/activity_horizontal_margin"

android:paddingRight="@dimen/activity_horizontal_margin"

android:paddingTop="@dimen/activity_vertical_margin"

app:layout_behavior="@string/appbar_scrolling_view_behavior"

tools:context="hr.app.liftme.liftmehr.AppLayoutMain"

android:orientation="vertical"

tools:showIn="@layout/activity_app_layout_main"

android:weightSum="1">

<LinearLayout

android:orientation="horizontal"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:id="@+id/gornjiLayoutIkone"

android:layout_marginBottom="5dp"

android:layout_marginTop="50dp">

<ImageButton

android:layout_width="wrap_content"

android:layout_height="110dp"

android:id="@+id/imageButtonVjezbe"

android:background="@drawable/ikona_vjezbe"

android:layout_weight="1"

android:layout_marginRight="8dp" />

<ImageButton

android:layout_width="wrap_content"

android:layout_height="110dp"

android:id="@+id/imageButtonProgramiTreninga"

android:background="@drawable/ikona_programi"

android:layout_weight="1"

android:layout_marginLeft="8dp"

android:layout_marginRight="8dp"

android:onClick="ProgramiTreninga"/>

<ImageButton

android:layout_width="wrap_content"

android:layout_height="110dp"

android:id="@+id/imageButtonPopisTeretana"

android:background="@drawable/ikona_popisteretana"

android:layout_weight="1"

android:layout_marginLeft="8dp" />

</LinearLayout>

<LinearLayout

android:orientation="horizontal"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:layout_below="@+id/gornjiLayoutIkone"

android:id="@+id/gornjiLayoutImena"

android:layout_marginBottom="5dp">

<TextView

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:textAppearance="?android:attr/textAppearanceMedium"

android:text="Vjebe"

android:id="@+id/textViewVjezbe"

android:layout_weight="1"

android:gravity="center_horizontal"

android:textColor="#000000" />

<TextView

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:textAppearance="?android:attr/textAppearanceMedium"

android:text="Programi \n treninga"

android:id="@+id/textViewProgramiTreninga"

android:layout_weight="1"

android:gravity="center_horizontal"

android:textColor="#000000" />

<TextView

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:textAppearance="?android:attr/textAppearanceMedium"

android:text="Popis \n teretana"

android:id="@+id/textViewPopisTeretana"

android:layout_weight="1"

android:gravity="center_horizontal"

android:textColor="#000000" />

</LinearLayout>

<LinearLayout

android:orientation="horizontal"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:layout_below="@+id/gornjiLayoutImena"

android:id="@+id/srednjiLayoutIkone"

android:layout_marginBottom="5dp">

<ImageButton

android:layout_width="wrap_content"

android:layout_height="110dp"

android:id="@+id/imageButtonPrehrana"

android:layout_weight="1"

android:layout_marginRight="8dp"

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

<ImageButton

android:layout_width="wrap_content"

android:layout_height="110dp"

android:id="@+id/imageButtonDodaciPrehrani"

android:layout_weight="1"

android:layout_marginLeft="8dp"

android:layout_marginRight="8dp"

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

<ImageButton

android:layout_width="wrap_content"

android:layout_height="110dp"

android:id="@+id/imageButtonTrgovineSportskomPrehranom"

android:layout_weight="1"

android:layout_marginLeft="8dp"

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

</LinearLayout>

<LinearLayout

android:orientation="horizontal"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:layout_below="@+id/srednjiLayoutIkone"

android:layout_marginBottom="5dp"

android:id="@+id/srednjiLayoutImena">

<TextView

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:textAppearance="?android:attr/textAppearanceMedium"

android:text="Prehrana"

android:id="@+id/textViewPrehrana"

android:layout_weight="1"

android:gravity="center_horizontal"

android:textColor="#000000" />

<TextView

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:textAppearance="?android:attr/textAppearanceMedium"

android:text="Dodaci \n prehrani"

android:id="@+id/textViewDodaciPrehrani"

android:layout_weight="1"

android:gravity="center_horizontal"

android:textColor="#000000" />

<TextView

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:textAppearance="?android:attr/textAppearanceMedium"

android:text="Trgovine \n sportskom \n prehranom"

android:id="@+id/textViewTrgovineSportskomPrehranom"

android:layout_weight="1"

android:gravity="center_horizontal"

android:textColor="#000000" />

</LinearLayout>

<LinearLayout

android:orientation="horizontal"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:layout_below="@+id/srednjiLayoutImena"

android:id="@+id/donjiLayoutIkone"

android:layout_marginBottom="5dp">

<ImageButton

android:layout_width="wrap_content"

android:layout_height="110dp"

android:id="@+id/imageButtonSavjeti"

android:layout_weight="1"

android:layout_marginRight="8dp"

android:background="@drawable/ikona_savjeti"

android:onClick="Savjeti"/>

<ImageButton

android:layout_width="wrap_content"

android:layout_height="110dp"

android:id="@+id/imageButtonKalkulatori"

android:layout_weight="1"

android:layout_marginLeft="8dp"

android:layout_marginRight="8dp"

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

<ImageButton

android:layout_width="wrap_content"

android:layout_height="110dp"

android:id="@+id/imageButtonKontakt"

android:layout_weight="1"

android:layout_marginLeft="8dp"

android:background="@drawable/ikona_kontakt"

android:onClick="Kontakt"/>

</LinearLayout>

<LinearLayout

android:orientation="horizontal"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:layout_below="@+id/donjiLayoutIkone"

android:id="@+id/donjiLayoutImena">

<TextView

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:textAppearance="?android:attr/textAppearanceMedium"

android:text="Savjeti"

android:id="@+id/textViewSavjeti"

android:layout_weight="1"

android:gravity="center_horizontal"

android:textColor="#000000" />

<TextView

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:textAppearance="?android:attr/textAppearanceMedium"

android:text="Kalkulatori"

android:id="@+id/textViewKalkulatori"

android:layout_weight="1"

android:gravity="center_horizontal"

android:textColor="#000000" />

<TextView

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:textAppearance="?android:attr/textAppearanceMedium"

android:text="Kontakt"

android:id="@+id/textViewKontakt"

android:layout_weight="1"

android:gravity="center_horizontal"

android:textColor="#000000" />

</LinearLayout>

</LinearLayout>

</ScrollView>

As you can see, main layout icons are OK, only text is not alligned properly. I don't know should I use layout:margin to align them, I think that won't be good because various screen sizes. Icons look good when there are exacly 3 in a row.

Screen-2

content_programi_treninga.xml

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

xmlns:app="http://schemas.android.com/apk/res-auto"

xmlns:tools="http://schemas.android.com/tools"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:paddingBottom="@dimen/activity_vertical_margin"

android:paddingLeft="@dimen/activity_horizontal_margin"

android:paddingRight="@dimen/activity_horizontal_margin"

android:paddingTop="@dimen/activity_vertical_margin"

app:layout_behavior="@string/appbar_scrolling_view_behavior"

tools:context="hr.app.liftme.liftmehr.ProgramiTreninga"

tools:showIn="@layout/activity_programi_treninga">

<LinearLayout

android:orientation="horizontal"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:id="@+id/linearLayout">

<ImageButton

android:layout_width="wrap_content"

android:layout_height="110dp"

android:id="@+id/imageButtonProgramiBodybuilding"

android:background="@drawable/ikona_programi_bodybuilding"

android:layout_weight="1"

android:adjustViewBounds="false"

android:layout_marginRight="15dp"

android:onClick="ProgramiBodybuilding"/>

<ImageButton

android:layout_width="wrap_content"

android:layout_height="110dp"

android:id="@+id/imageButtonProgramiPowerlifting"

android:background="@drawable/ikona_programi_powerlifting"

android:layout_weight="1"

android:adjustViewBounds="false"

android:layout_marginLeft="15dp"

android:onClick="ProgramiPowerlifting"

/>

</LinearLayout>

<LinearLayout

android:orientation="horizontal"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:layout_below="@+id/linearLayout"

android:id="@+id/linearLayout2">

<TextView

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:textAppearance="?android:attr/textAppearanceMedium"

android:text="Bodybuilding"

android:id="@+id/textViewProgramiBodybuilding"

android:textColor="#000000"

android:gravity="center_horizontal"

android:layout_weight="1"

android:layout_marginTop="5dp"

android:onClick="ProgramiBodybuilding"/>

<TextView

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:textAppearance="?android:attr/textAppearanceMedium"

android:text="Powerlifting"

android:id="@+id/textView8"

android:textColor="#000000"

android:gravity="center_horizontal"

android:layout_weight="1"

android:layout_marginTop="5dp"

android:onClick="ProgramiPowerlifting"/>

</LinearLayout>

<LinearLayout

android:orientation="horizontal"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:layout_below="@+id/linearLayout2"

android:gravity="center_horizontal"

android:layout_marginBottom="5dp"

android:id="@+id/linearLayout3">

<ImageButton

android:layout_width="115dp"

android:layout_height="115dp"

android:id="@+id/imageButtonPowerbuilding"

android:background="@drawable/ikona_programi_powerbuilding"

android:layout_weight="0"

android:layout_marginTop="10dp"

android:onClick="ProgramiPowerBuilding"/>

</LinearLayout>

<LinearLayout

android:orientation="horizontal"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:layout_below="@+id/linearLayout3"

android:gravity="center_horizontal">

<TextView

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:textAppearance="?android:attr/textAppearanceMedium"

android:text="Power \nBuilding"

android:id="@+id/textView7"

android:textColor="#000000"

android:gravity="center" />

</LinearLayout>

</RelativeLayout>

As you can see, it doesn't look good with 2 in a row.

I created various icon sizes, from ldpi screen to xxxhdpi screen and when I add them on my layout editor, they are so small so I am using layout:height to correct that. But I don't think that's a good idea because of different screen sizes.

So my question is, what should I do to fix this so it would be perfectly alligned and imageButtons big and in 1:1 ratio?

If you need me to upload some code, just tell me what code and I'll upload it ASAP!

Thanks!

分享给朋友:
您可能感兴趣的文章:
随机阅读: