ImageButton 在模拟器或物理设备上的 Android Studio 中不可见
ImageButton not visible on Android Studio on Emulator or physical device
伟大的社区,您好。我是 Android 的新手,正在尝试构建一个计算器,这是我第一次遇到这个问题,不确定哪里出了问题。我添加了 buttons
,除了 image button
,它们在模拟器上看起来很好。代码如下,希望得到帮助。
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingTop="10dp"
android:weightSum="4">
<Button
android:id="@+id/button16"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_weight="1"
android:background="#00000000"
android:text="7"
android:textColor="@android:color/background_dark"
android:textSize="30sp"/>
<Button
android:id="@+id/button5"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_weight="1"
android:background="#00000000"
android:text="8"
android:textColor="@android:color/background_dark"
android:textSize="30sp"/>
<Button
android:id="@+id/button4"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_weight="1"
android:background="#00000000"
android:text="9"
android:textColor="@android:color/background_dark"
android:textSize="30sp"/>
<ImageButton
android:id="@+id/imageButton"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_weight="1"
android:background="#00000000"
android:textColor="@android:color/background_dark"
app:srcCompat="@drawable/divide"/>
</LinearLayout>
最后一个除法按钮不可见,我添加了四个这样的线性布局,分别是 1、2、3、减、4、5、6、乘法按钮,所有这些乘法、除法、加法和减法 buttons
不幸的是不可见。
我才刚刚起步,希望知道我错在哪里以及我需要修复什么。
更新
见我附上的上图。在这个模拟器中可见的Views
是Buttons
如果我把它们改成ImageButton
和views
之类的等号。我已经更改了我的 png's
文件,但我无法理解我做错了什么
谢谢
如果使用权重,请使用 0dp 的宽度(或垂直 LinearLayout 的高度)。
android:layout_width = "0dp"
android:layout_height ="80dp"
android:layout_weight ="1"
您是否在 Java class 中声明了按钮,而且您的 Drawable 中有 png 格式的文件,这是处理 img 文件的最佳方式。
我已经解决了这个问题,现在可以使用了,因为我只更改了一行,你的每个 ImageButton
是 android:src="@drawable/equalsymbol"
而不是我已经提到的 app:srcCompat="@drawable/equalsymbol"
你在评论区
请参阅下面的屏幕截图-:
编辑
现在,问题是为什么 app.srcCompat
不工作
这是因为您有 extend
您的 MainAcyivity.java
和 Activity
你必须 Extend
和 AppCompatActivity
和需要更新您的gradle.build
您必须添加以下行 -:
vectorDrawables {
useSupportLibrary = true
}
查看详细答案
然后,我再次对您的应用程序进行一些更改
改1
MainActivity.java
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
变2
style.xml
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.DayNight.NoActionBar">
<!-- Customize your theme here. -->
<!--<item name="android:colorPrimary">@color/colorPrimary</item>-->
<!--<item name="android:colorPrimaryDark">@color/colorPrimaryDark</item>-->
<!--<item name="android:colorAccent">@color/colorAccent</item>-->
</style>
变3
build.gradle
android {
defaultConfig {
vectorDrawables {
useSupportLibrary = true
}
}
}
现在,您可以在您的应用程序中使用 app:srcCompat="@drawable/equalsymbol"
看,下面的第二个屏幕截图我已经使用了 app:srcCompat
,现在可以使用了
现在,一切正常。
因此,按照上述进行更改。快乐编码
伟大的社区,您好。我是 Android 的新手,正在尝试构建一个计算器,这是我第一次遇到这个问题,不确定哪里出了问题。我添加了 buttons
,除了 image button
,它们在模拟器上看起来很好。代码如下,希望得到帮助。
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingTop="10dp"
android:weightSum="4">
<Button
android:id="@+id/button16"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_weight="1"
android:background="#00000000"
android:text="7"
android:textColor="@android:color/background_dark"
android:textSize="30sp"/>
<Button
android:id="@+id/button5"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_weight="1"
android:background="#00000000"
android:text="8"
android:textColor="@android:color/background_dark"
android:textSize="30sp"/>
<Button
android:id="@+id/button4"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_weight="1"
android:background="#00000000"
android:text="9"
android:textColor="@android:color/background_dark"
android:textSize="30sp"/>
<ImageButton
android:id="@+id/imageButton"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_weight="1"
android:background="#00000000"
android:textColor="@android:color/background_dark"
app:srcCompat="@drawable/divide"/>
</LinearLayout>
最后一个除法按钮不可见,我添加了四个这样的线性布局,分别是 1、2、3、减、4、5、6、乘法按钮,所有这些乘法、除法、加法和减法 buttons
不幸的是不可见。
我才刚刚起步,希望知道我错在哪里以及我需要修复什么。
更新
见我附上的上图。在这个模拟器中可见的Views
是Buttons
如果我把它们改成ImageButton
和views
之类的等号。我已经更改了我的 png's
文件,但我无法理解我做错了什么
谢谢
如果使用权重,请使用 0dp 的宽度(或垂直 LinearLayout 的高度)。
android:layout_width = "0dp"
android:layout_height ="80dp"
android:layout_weight ="1"
您是否在 Java class 中声明了按钮,而且您的 Drawable 中有 png 格式的文件,这是处理 img 文件的最佳方式。
我已经解决了这个问题,现在可以使用了,因为我只更改了一行,你的每个 ImageButton
是 android:src="@drawable/equalsymbol"
而不是我已经提到的 app:srcCompat="@drawable/equalsymbol"
你在评论区
请参阅下面的屏幕截图-:
编辑
现在,问题是为什么 app.srcCompat
不工作
这是因为您有 extend
您的 MainAcyivity.java
和 Activity
你必须 Extend
和 AppCompatActivity
和需要更新您的gradle.build
您必须添加以下行 -:
vectorDrawables {
useSupportLibrary = true
}
查看详细答案
然后,我再次对您的应用程序进行一些更改
改1
MainActivity.java
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
变2
style.xml
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.DayNight.NoActionBar">
<!-- Customize your theme here. -->
<!--<item name="android:colorPrimary">@color/colorPrimary</item>-->
<!--<item name="android:colorPrimaryDark">@color/colorPrimaryDark</item>-->
<!--<item name="android:colorAccent">@color/colorAccent</item>-->
</style>
变3
build.gradle
android {
defaultConfig {
vectorDrawables {
useSupportLibrary = true
}
}
}
现在,您可以在您的应用程序中使用 app:srcCompat="@drawable/equalsymbol"
看,下面的第二个屏幕截图我已经使用了 app:srcCompat
,现在可以使用了
现在,一切正常。
因此,按照上述进行更改。快乐编码