旋转我的应用程序时,评级栏显示更多星星

时间:2016-03-23 18:43:58

标签: java android android-studio

我的评级栏明星有这个奇怪的问题。

This is my app in vertical position

This is my app in horizontal position

正如您所见,星星完全改变

这是我的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:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:background="@drawable/main"
    tools:context="com.example.draganam.blankapp.Nivo1Activity">

    <TableLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:shrinkColumns="1,0" android:stretchColumns="0,1">

        <TableRow
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:paddingTop="50dp">

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/slika0"
                android:src="@drawable/becutan1"
                android:layout_column="0"
                android:layout_marginRight="40px" />

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/slika1"
                android:src="@drawable/blizoo1"
                android:layout_column="1" />
        </TableRow>

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

            <RatingBar
                android:id="@+id/rb0"
                style="?android:attr/ratingBarStyleIndicator"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:numStars="5"
                android:stepSize="1.0"
                android:isIndicator="true"
                android:scaleX="0.7"
                android:scaleY="0.7"/>

            <RatingBar
                android:id="@+id/rb1"
                style="?android:attr/ratingBarStyleIndicator"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:numStars="5"
                android:stepSize="1.0"
                android:isIndicator="true"
                android:scaleX="0.7"
                android:scaleY="0.7"/>
        </TableRow>

        <TableRow
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:paddingTop="50dp">

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/slika2"
                android:src="@drawable/brilijant1"
                android:layout_column="0"
                android:layout_marginRight="40px" />

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/slika3"
                android:src="@drawable/vitaminka1"
                android:layout_column="1" />
        </TableRow>
        <TableRow
            android:layout_width="match_parent"
            android:layout_height="match_parent">
        <RatingBar
            android:id="@+id/rb2"
            style="?android:attr/ratingBarStyleIndicator"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:numStars="5"
            android:stepSize="1.0"
            android:isIndicator="true"
            android:scaleX="0.7"
            android:scaleY="0.7"/>

        <RatingBar
            android:id="@+id/rb3"
            style="?android:attr/ratingBarStyleIndicator"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:numStars="5"
            android:stepSize="1.0"
            android:isIndicator="true"
            android:scaleX="0.7"
            android:scaleY="0.7"/>
    </TableRow>

    </TableLayout>
</RelativeLayout>

这是我的java代码:

Context ctx2 = this;
String[][] arrayWithInformations = new String[5][];
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_nivo1);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);

    DatabaseOperations databaseOperations = new DatabaseOperations(ctx2);
    Cursor cursorLogo = databaseOperations.getInformationsForLogos(databaseOperations);
    // looping through all rows and adding to list
    Integer i = 0;
    if (cursorLogo.moveToFirst()) {
        do {
            String[] helperArray = {cursorLogo.getString(0),cursorLogo.getString(1),cursorLogo.getString(2),cursorLogo.getString(3),
                    cursorLogo.getString(4),cursorLogo.getString(5)};
            arrayWithInformations[i] = helperArray;
            i++;

        } while (cursorLogo.moveToNext());
    }
    int firstRating = Integer.parseInt(arrayWithInformations[0][4]);
    int secondRating = Integer.parseInt(arrayWithInformations[1][4]);
    int thirdRating = Integer.parseInt(arrayWithInformations[2][4]);
    int fourthRating = Integer.parseInt(arrayWithInformations[3][4]);
    RatingBar rb0 = (RatingBar) findViewById(R.id.rb0);
    rb0.setRating(firstRating);
    RatingBar rb1 = (RatingBar) findViewById(R.id.rb1);
    rb1.setRating(secondRating);
    RatingBar rb2 = (RatingBar) findViewById(R.id.rb2);
    rb2.setRating(thirdRating);
    RatingBar rb3 = (RatingBar) findViewById(R.id.rb3);
    rb3.setRating(fourthRating);

我从数据库文件中获得评级值,一切正常,星星还可以,但是当我旋转手机时,他们都错了。我知道that当您旋转手机时,当前活动会再次加载,但它是相同的代码并且加载错误。也许它正在成倍增加星星......我听起来可能很有趣,但我不知道出了什么问题。有人请给我带头..

1 个答案:

答案 0 :(得分:1)

我发现了问题!

  

机器人:shrinkColumns =&#34; 1,0&#34;机器人:stretchColumns =&#34; 0,1&#34;

将此设置为 TableLayout 会导致这个奇怪的问题。

感谢大家有兴趣帮助我..