【问题标题】:Android can't see one of the buttonsAndroid 看不到其中一个按钮
【发布时间】:2016-07-23 17:20:49
【问题描述】:

在我的项目中,我使用了一些按钮(2 个按钮),问题是我在虚拟设备和真实设备上看不到其中一个按钮(设置按钮),但在设计模式中我可以看到按钮。如果有人可以帮助我解决这个问题,我会很高兴。 谢谢。

Xml -

<?xml version="1.0" encoding="utf-8"?>
  <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="aa.po.MainActivity">


    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:text="My Tasks"
        android:background="#e2e2e2" />

    <ListView
        android:id="@+id/list_todo"
        tools:listitem="@layout/item_todo"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

<Button
    android:layout_width="60dp"
    android:layout_height="60dp"
    android:id="@+id/add_task"
    android:src="@drawable/addtask"
    android:background="@drawable/addtask"
    android:layout_alignParentTop="true"
    android:layout_alignParentStart="true"
    android:layout_marginStart="102dp" />
<Button
    android:layout_width="40dp"
    android:layout_height="40dp"
    android:src="@drawable/settings"
    android:background="@drawable/settings"
    android:layout_alignParentBottom="true"
    android:layout_alignParentEnd="true"
    android:onClick="ButtonClick"
    android:layout_marginEnd="6dp"
    android:layout_marginBottom="90dp"
    android:id="@+id/settings" />

</RelativeLayout>
</FrameLayout>

【问题讨论】:

  • 使用 LinearLayout 代替 RelativeLayout。
  • 发布布局图片
  • @Vucko 对不起,但没有改变任何东西
  • @SaravInfern 我添加了布局的图片
  • 硬编码按钮的大小和边距可能会导致问题...

标签: android xml button


【解决方案1】:

请使用下面的 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="match_parent"
    android:layout_height="match_parent"
    tools:context="aa.po.MainActivity">


    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:text="My Tasks"
        android:background="#e2e2e2" />

    <ListView
        android:id="@+id/list_todo"
        tools:listitem="@layout/item_todo"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

<Button
    android:layout_width="60dp"
    android:layout_height="60dp"
    android:id="@+id/add_task"
    android:background="@drawable/addtask"
    android:layout_alignParentBottom="true"
    android:layout_alignParentEnd="true" <-- if your min sdk is below 17 useandroid:layout_alignParentRight ="true" instead
    android:layout_margin="20dp" />
<Button
    android:layout_width="40dp"
    android:layout_height="40dp"
    android:background="@drawable/settings"
    android:onClick="ButtonClick"
    android:layout_above="@+id/add_task"
    android:layout_alignParentEnd="true" <-- if your min sdk is below 17 useandroid:layout_alignParentRight ="true" instead
    android:id="@+id/settings" />

</RelativeLayout>

【讨论】:

    【解决方案2】:

    你可以在你的 onCreate() 方法中试试这个

    Button task= (Button)findViewById(R.id.add_task);
    Button settings= (Button)findViewById(R.id.settings);
    
    task.setVisibility(View.VISIBLE);
    settings.setVisibility(View.VISIBLE);
    

    【讨论】:

    • 我在空对象引用上收到此错误“'void android.widget.Button.setVisibility(int)'”但我不知道为什么
    • @kobipopo1 因为 findViewById 返回 null
    • @cricket_007 但问题是为什么返回 null ?
    • @kobipopo1 通常是因为您使用错误的 XML 文件膨胀或 setContentView,因此无法找到这些 id 值,因此它们返回为 null
    • @cricket_007 你是对的,可能这个人使用了错误的 xml
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多