繁体   English   中英

为什么我的一些 TextViews 和 ImageViews 在执行应用程序时没有出现?

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

我有 android 布局,带有 4 个 imageViews 和 6 个 TextViews,其中 4 个按预期显示,但所有元素的 rest 都没有出现。 我的布局有以下脚本:

    <?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>

在 Android Studio 上输出预期的布局:

预期结果

但运行该应用程序会产生以下 output:

结果

无法弄清楚是什么原因造成的,因为它看起来像是随机出现的 TextViews。 我不知道它是否与 Java Class 有任何关系,但我认为不是,无论哪种方式,我都是这样做的:

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);
            }
        }

}

我尝试更改我使用的图像并重新启动并清除缓存几次。 结果还是一样。

您必须在代码中使用app:srcCompatxmlimageview.setImageResource()中。

tools:srcCompat="@tools:sample/avatars"仅在xml中显示预览。

暂无
暂无

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

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