如何在多种屏幕设备上调整Android应用?

时间:2016-10-27 07:37:48

标签: android android-layout compatibility

我正在开发一个关于提供Pokémons信息的Android应用程序。

然而,当在不同设备之间切换时,我遇到了很多麻烦。

在此屏幕截图中 enter image description here

你可以清楚地看到应用程序在Nexus 5X上运行良好,但在Nexus 5上,布局完全混乱(如果你注意到我在每台设备上都有Android API 23)。

我知道这是不同设备的像素密度问题,因为Nexus 5有xxhdpi显示屏,而5X在xhdpi和xxhdpi之间有一种“混合显示”。我知道三星Galaxy S6和华为P9 Lite都有相同的Nexus 5问题。 我试图调整每个图像的大小,制作drawable-ldpi,drawable-mdpi ...文件夹,但结果只是Android Studio不再看到我的图片了! >。<

请帮帮我,真的不知道该怎么办!

我的布局XML文件:

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

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/pokedetails"
    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"
    tools:context="com.example.thefe.newsmartkedex.MainActivity">

<TableLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/pokeImgName">
    <TableRow>
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/tmppkmn" />
    </TableRow>
    <TableRow>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAlignment="center"
            android:id="@+id/pkmnName"
            android:textSize="20sp"
            android:textStyle="bold" />
    </TableRow>
</TableLayout>
<TableLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/pokeTypes"
    android:layout_alignParentEnd="true"
    android:layout_marginTop="70dp">
    <TableRow>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Tipi"
            android:textAlignment="center"
            android:textSize="20sp"/>
    </TableRow>
    <TableRow>
        <ImageView
            android:layout_width="80dp"
            android:layout_height="26dp"
            android:id="@+id/tipo1" />
    </TableRow>
    <TableRow>
        <ImageView
            android:layout_width="80dp"
            android:layout_height="26dp"
            android:id="@+id/tipo2"/>
    </TableRow>
    <TableRow android:id="@+id/capturedSwitch">
       <Switch
           android:textStyle="bold|italic"
           android:layout_gravity="center_horizontal"
           android:id="@+id/dettagli"/>
    </TableRow>
    <TableRow android:id="@+id/capturedButton">
        <Button
            android:textSize="11dp"
            android:layout_gravity="center_horizontal"
            android:id="@+id/catturato"/>
    </TableRow>
</TableLayout>

    <TextView
        android:text="Forte contro"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_alignParentStart="true"
        android:textSize="20sp"
        android:id="@+id/forteContro" />

    <TableLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/forteContro"
        android:id="@+id/pokeStrenght">

        <TableRow
            android:padding="10dp">
            <ImageView
                android:layout_width="80dp"
                android:layout_height="26dp"
                android:id="@+id/tsf1"
                android:src="@drawable/veleno"/>
            <ImageView
                android:layout_width="80dp"
                android:layout_height="26dp"
                android:id="@+id/tsf2"
                android:src="@drawable/acciaio"/>
            <ImageView
                android:layout_width="80dp"
                android:layout_height="26dp"
                android:id="@+id/tsf3"
                android:src="@drawable/fuoco"/>
            <ImageView
                android:layout_width="80dp"
                android:layout_height="26dp"
                android:id="@+id/tsf4"
                android:src="@drawable/erba"/>
        </TableRow>
    </TableLayout>

<TextView
    android:text="Debole contro"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/pokeStrenght"
    android:textSize="20sp"
    android:id="@+id/deboleContro" />

<TableLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/deboleContro"
    android:id="@+id/pokeWeakness">

    <TableRow
        android:padding="10dp">
        <ImageView
            android:layout_width="80dp"
            android:layout_height="26dp"
            android:id="@+id/tsd1"
            android:src="@drawable/acqua"/>
        <ImageView
            android:layout_width="80dp"
            android:layout_height="26dp"
            android:id="@+id/tsd2"
            android:src="@drawable/psico"/>
        <ImageView
            android:layout_width="80dp"
            android:layout_height="26dp"
            android:id="@+id/tsd3"
            android:src="@drawable/lotta"/>
        <ImageView
            android:layout_width="80dp"
            android:layout_height="26dp"
            android:id="@+id/tsd4"
            android:src="@drawable/terra"/>
    </TableRow>
</TableLayout>

    <TableLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/pokeWeakness"
        android:id="@+id/descriptionTable">

        <TableRow
            android:padding="2dp">
            <Button
                android:text="Descrizione"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="11sp"
                android:layout_gravity="left"
                android:id="@+id/showhidedescr"/>
        </TableRow>
        <TableRow
            android:padding="2dp">
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/descriptiontext"
                android:textSize="11dp"
                android:layout_gravity="center_horizontal">
            </TextView>
        </TableRow>
        <TableRow
            android:padding="2dp">
            <Button
                android:layout_height="wrap_content"
                android:layout_width="wrap_content"
                android:text="Leggi\ndescrizione"
                android:textSize="11sp"
                android:id="@+id/leggidescrizione"
                android:layout_gravity="center_horizontal">
            </Button>
        </TableRow>
    </TableLayout>

 和我的Java文件:

public class PokemonDetails extends AppCompatActivity implements WebServicesAsyncResponse {

private TextView tv;
private TextToSpeech t1;
private String toSpeech = "";

@Override
public void onCreate (Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.pokedetails);

    tv = (TextView) findViewById(R.id.descriptiontext);

    //Prendo i dati Intent
    Intent i = getIntent();

    //Seleziono l'ID del Pokémon che mi servirà per prendere tutti i dati dal database e dai drawable
    final int pokeID = i.getExtras().getInt("id");
    String pokeName = getName(pokeID+1);

    PokemonDatabaseAdapter pokemonHelper = new PokemonDatabaseAdapter(this);

    ResponseFromWebService responseFromWebService = new ResponseFromWebService();
    WebServicesAsyncResponse ar = this;
    responseFromWebService.getPokeData(pokeName, ar);

    ImageAdapter imageAdapter = new ImageAdapter(this);

    ImageView imageView = (ImageView) findViewById(R.id.tmppkmn);
    imageView.setImageResource(imageAdapter.mThumbIds[pokeID]);
    imageView.setContentDescription("Image of the current Pokémon, "+pokeName);

    imageView = (ImageView) findViewById(R.id.tipo1);
    imageView.setImageResource(R.drawable.erba);

    imageView = (ImageView) findViewById(R.id.tipo2);
    imageView.setImageResource(R.drawable.veleno);

    TextView pkmnName = (TextView)findViewById(R.id.pkmnName);
    pkmnName.setText(pokeName);

    final Switch pokeSwitch = (Switch) findViewById(R.id.dettagli);
    final Button pokeDetails = (Button) findViewById(R.id.catturato);

    if (pokemonHelper.getPokemonGO() == 1) {
        pokeSwitch.setText("Catturato  ");
        pokeDetails.setText("Aggiungi\nDettagli");

        //prendendo i dati dal database, pokeDetails dev'essere enabled o disabled
        pokeDetails.setEnabled(false);

        pokeSwitch.setOnCheckedChangeListener(new OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (pokeSwitch.isChecked())
                    pokeDetails.setEnabled(true);
                else
                    pokeDetails.setEnabled(false);
            }
        });

        pokeDetails.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent i = new Intent(getApplicationContext(), MyPokeDetails.class);
                i.putExtra("id", pokeID);
                startActivity(i);
            }
        });
    }
    else {
        ViewGroup layout = (ViewGroup) pokeDetails.getParent();
        layout.removeView(pokeDetails);
        layout = (ViewGroup) pokeSwitch.getParent();
        layout.removeView(pokeSwitch);
    }

    getActionBar();

    Button showhide = (Button) findViewById(R.id.showhidedescr);
    final Button leggi = (Button)findViewById(R.id.leggidescrizione);
    tv.setVisibility(View.GONE);
    leggi.setVisibility(View.GONE);
    showhide.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (tv.getVisibility() == View.VISIBLE) {
                tv.setVisibility(View.GONE);
                leggi.setVisibility(View.GONE);
            }
            else if (tv.getVisibility() == View.GONE) {
                tv.setVisibility(View.VISIBLE);
                leggi.setVisibility(View.VISIBLE);
            }
        }
    });

    leggi.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            t1=new TextToSpeech(getApplicationContext(), new TextToSpeech.OnInitListener() {
                public void onInit(int status) {
                    if(status != TextToSpeech.ERROR) {
                        t1.setLanguage(Locale.ITALIAN);
                        t1.speak(toSpeech, TextToSpeech.QUEUE_FLUSH, null);
                    }
                }
            });
        }
    });
}

对不起代码中的所有意大利语评论,感谢您给我的任何帮助!

1 个答案:

答案 0 :(得分:1)

使用productFlavours可以解决此问题,您可以根据需要编写单独的风格。

示例:

productFlavors {
        phone {

        }
        tablet {

        }
    }

如果您想了解有关此主题的更多信息,请参阅此博客http://www.techotopia.com/index.php/An_Android_Studio_Gradle_Build_Variants_Example