简体   繁体   中英

Why some of my TextViews and ImageViews not appear when executing app?

I have and android layout with 4 imageViews and 6 TextViews, and 4 of them appear as expected but all the rest of the elements do not appear. My layout has the following script:

    <?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.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"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <include
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        layout="@layout/toolbar"/>
    <com.google.android.material.navigation.NavigationView
        android:id="@+id/navigationView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:headerLayout="@layout/drawer_header"
        app:menu="@menu/drawer_menu"
        android:layout_gravity="start">

    </com.google.android.material.navigation.NavigationView>

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <TextView
            android:id="@+id/liveFeeds"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="48dp"
            android:layout_marginLeft="48dp"
            android:layout_marginTop="64dp"
            android:textColor="@android:color/black"
            android:textSize="36sp"
            android:textStyle="bold"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            tools:text="@string/livefeeds" />

        <TextView
            android:id="@+id/livefeedsInfo"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="48dp"
            android:layout_marginLeft="48dp"
            android:layout_marginTop="44dp"
            android:textColor="@android:color/black"
            android:textSize="18sp"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/liveFeeds"
            tools:text="@string/liveFeedsText" />

        <ImageView
            android:id="@+id/cameraOne"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="52dp"
            android:layout_marginLeft="52dp"
            android:layout_marginTop="56dp"
            android:contentDescription="@string/camera_one"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/livefeedsInfo"
            tools:srcCompat="@tools:sample/avatars" />

        <ImageView
            android:id="@+id/cameraTwo"
            android:layout_width="137dp"
            android:layout_height="127dp"
            android:layout_marginEnd="52dp"
            android:layout_marginRight="52dp"
            android:layout_marginBottom="366dp"
            android:contentDescription="@string/camera_two"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.976"
            app:layout_constraintStart_toEndOf="@+id/cameraOne"
            tools:srcCompat="@tools:sample/backgrounds/scenic" />

        <ImageView
            android:id="@+id/cameraThree"
            android:layout_width="136dp"
            android:layout_height="128dp"
            android:layout_marginStart="52dp"
            android:layout_marginLeft="52dp"
            android:layout_marginBottom="132dp"
            android:contentDescription="@string/camera_three"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            tools:srcCompat="@tools:sample/avatars[4]" />

        <ImageView
            android:id="@+id/cameraFour"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="132dp"
            android:contentDescription="@string/camera_four"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.442"
            app:layout_constraintStart_toEndOf="@+id/cameraThree"
            tools:srcCompat="@tools:sample/avatars" />

        <TextView
            android:id="@+id/cameraOneName"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="80dp"
            android:layout_marginLeft="80dp"
            android:layout_marginTop="32dp"
            android:text="@string/camera_one"
            android:textSize="18sp"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/cameraOne" />

        <TextView
            android:id="@+id/cameraTwoName"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginEnd="76dp"
            android:layout_marginRight="76dp"
            android:text="@string/camera_two"
            android:textSize="18sp"
            app:layout_constraintBaseline_toBaselineOf="@+id/cameraOneName"
            app:layout_constraintEnd_toEndOf="parent" />

        <TextView
            android:id="@+id/cameraThreeName"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="88dp"
            android:layout_marginLeft="88dp"
            android:text="@string/camera_three"
            android:textSize="18sp"
            app:layout_constraintBaseline_toBaselineOf="@+id/cameraFourName"
            app:layout_constraintStart_toStartOf="parent" />

        <TextView
            android:id="@+id/cameraFourName"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="32dp"
            android:layout_marginEnd="76dp"
            android:layout_marginRight="76dp"
            android:text="@string/camera_four"
            android:textSize="18sp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/cameraFour" />


    </androidx.constraintlayout.widget.ConstraintLayout>

</androidx.drawerlayout.widget.DrawerLayout>

Which outputs the expected layout on Android Studio:

预期结果

But running the app produces the following output:

结果

Can't figure out what's causing this, since it looks like random the TextViews that appear. I don't know if it has anything to do with the Java Class, but I think not, either way, here's how I made it:

public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
    DrawerLayout drawerLayout;
    ActionBarDrawerToggle drawerToggle;
    Toolbar toolbar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        // drawer menu settings
        drawerLayout = findViewById(R.id.drawer);
        drawerToggle = new ActionBarDrawerToggle(this,
                drawerLayout, toolbar, open_drawer, R.string.close_drawer);
        drawerLayout.addDrawerListener(drawerToggle);
        drawerToggle.setDrawerIndicatorEnabled(true); // enable hamburger
        NavigationView navigationView = findViewById(R.id.navigationView);
        navigationView.setNavigationItemSelectedListener(this);
        drawerToggle.syncState();


        //Parte de Zoom e edição de nome da camera
        TextView screenName = (TextView)findViewById(R.id.liveFeeds);
        final ImageView cameraOne =(ImageView)findViewById(R.id.cameraOne);
        ImageView cameraTwo=(ImageView)findViewById(R.id.cameraTwo);
        ImageView cameraThree=(ImageView)findViewById(R.id.cameraThree);
        ImageView cameraFour=(ImageView)findViewById(R.id.cameraFour);


        cameraOne.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //zoomedImage.setImageResource(cameraOne);
            }
        });

        cameraTwo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //zoomedImage.setImageResource(cameraTwo);
            }
        });

        cameraThree.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //zoomedImage.setImageResource(cameraThree);
            }
        });

        cameraFour.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //zoomedImage.setImageResource(cameraFour);
            }
        });

    }
    @Override
    public boolean onNavigationItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.settings:
                Intent intent = new Intent(MainActivity.this, SettingsActivity.class);
                this.startActivity(intent);
                return true;
            default:
                return super.onOptionsItemSelected(item);
            }
        }

}

I've tried changing the images I use and restarted and cleared cache a couple times. Still same result.

you must use app:srcCompat in xml or imageview.setImageResource() in code .

tools:srcCompat="@tools:sample/avatars" only shows a preview in xml .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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