繁体   English   中英

Android:使应用程序适应具有包括其他布局在内的布局的小屏幕

[英]Android: Adapting app to small screens with layouts including other layouts

我使用“布局”文件夹中的所有xml布局来完成我的应用程序,该文件夹用于普通尺寸的屏幕。 现在,我从小屏幕开始,在“ res”中“ layout”文件夹下创建一个名为“ layout-small”的文件夹。

我对较小的布局进行了一些修改,并使用了较小的按钮和页边距,然后尝试在普通屏幕和小型屏幕上运行它,以查看每个屏幕是否都将使用应使用的布局,但都使用了正常布局。 小屏幕不使用小布局。

我相信这是由于使用setContent()方法的Java类使用了一个布局,而该布局又包括另一个布局,该布局也包括第三个布局。

这是我的代码,更加清晰。 3个activity_main.xml app_bar_main.xml和content_main.xml布局文件分别位于res / layout和res / layout-small中,仅content_main.xml中的按钮大小有所不同。

Home.java:

public class Home extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {

/**fields*/
private Button butVentes, butLocations, butRecherche, butFavoris, butContact, butSocial;   //Buttons for home screen
private Toolbar toolbar;
private DrawerLayout drawer;
private NavigationView navigationView;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    getWindow().setFormat(PixelFormat.RGBA_8888);
    setContentView(R.layout.activity_main);

    /**toolbar*/
    toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    /**drawer*/
    drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
    drawer.addDrawerListener(toggle);
    toggle.syncState();

    /**navigationView*/
    navigationView = (NavigationView) findViewById(R.id.nav_view);
    navigationView.setNavigationItemSelectedListener(this);
}

/*...*/

}

activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout 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:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">

<include
    layout="@layout/app_bar_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

<android.support.design.widget.NavigationView
    android:id="@+id/nav_view"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:fitsSystemWindows="true"
    app:headerLayout="@layout/nav_header_main"
    app:menu="@menu/activity_main_drawer" />

app_bar_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 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:fitsSystemWindows="true"
tools:context=".MainActivities.Home">

<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/AppTheme.AppBarOverlay">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:popupTheme="@style/AppTheme.PopupOverlay" />

</android.support.design.widget.AppBarLayout>

<include layout="@layout/content_main" />

content_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<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:id="@+id/home_background_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/app_accueil_background"
android:orientation="horizontal"
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=".MainActivities.Home"
tools:showIn="@layout/app_bar_main">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center|bottom"
    android:layout_marginBottom="40dp"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginTop="240dp"
        android:orientation="horizontal">

        <Button
            android:id="@+id/buttonVentes"
            android:layout_width="60dp"
            android:layout_height="48dp"
            android:layout_marginRight="5dp"
            android:background="@drawable/circle"
            android:drawableTop="@drawable/ic_view_list"
            android:paddingTop="10dp"
            android:text="@string/content_main_vente"
            android:textColor="@color/colorBlackText"
            android:textSize="13sp" />

        <Button
            android:id="@+id/buttonLocations"
            android:layout_width="60dp"
            android:layout_height="48dp"
            android:layout_marginRight="5dp"
            android:background="@drawable/circle"
            android:drawableTop="@drawable/ic_view_list"
            android:paddingTop="10dp"
            android:text="@string/content_main_location"
            android:textColor="@color/colorBlackText"
            android:textSize="13sp" />

        <Button
            android:id="@+id/buttonRecherche"
            android:layout_width="60dp"
            android:layout_height="48dp"
            android:background="@drawable/circle"
            android:drawableTop="@drawable/ic_search"
            android:paddingTop="10dp"
            android:text="@string/content_main_recherche"
            android:textColor="@color/colorBlackText"
            android:textSize="13sp" />


    </LinearLayout>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginTop="40dp"
        android:orientation="horizontal">

        <Button
            android:id="@+id/buttonFavoris"
            android:layout_width="100dp"
            android:layout_height="80dp"
            android:layout_marginRight="14dp"
            android:background="@drawable/circle"
            android:drawableTop="@drawable/ic_star_accueil"
            android:paddingTop="10dp"
            android:text="@string/content_main_favoris"
            android:textColor="@color/colorBlackText"
            android:textSize="13sp" />

        <Button
            android:id="@+id/buttonContact"
            android:layout_width="100dp"
            android:layout_height="80dp"
            android:layout_marginRight="14dp"
            android:background="@drawable/circle"
            android:drawableTop="@drawable/ic_contacts"
            android:paddingTop="10dp"
            android:text="@string/content_main_contact"
            android:textColor="@color/colorBlackText"
            android:textSize="13sp" />

        <Button
            android:id="@+id/buttonSocial"
            android:layout_width="100dp"
            android:layout_height="80dp"
            android:background="@drawable/circle"
            android:drawableTop="@drawable/ic_share"
            android:paddingTop="10dp"
            android:text="@string/content_main_social"
            android:textColor="@color/colorBlackText"
            android:textSize="13sp" />


    </LinearLayout>

</LinearLayout>

无需创建其他布局文件夹,只需维护不同的尺寸文件夹,例如:
值-sw320dp-1dp
值-sw360dp-1.12dp
值-sw410dp-1.28dp
值-sw480dp-1.5dp

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM