![](/img/trans.png)
[英]Tried to replace an emulator with a physical device (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类中声明了botton,而且Drawable在png中有文件,这是使用img文件的最佳方法。
我已经解决了这个问题,现在可以正常工作了,因为我只更改了每个ImageButton
一行,是android:src="@drawable/equalsymbol"
而不是我已经提到的app:srcCompat="@drawable/equalsymbol"
在评论部分给您
请参阅下面的屏幕截图:
编辑
现在,问题是为什么app.srcCompat
无法正常工作
这是因为您通过Activity
extend
了MainAcyivity.java
您必须使用AppCompatActivity
进行Extend
并需要更新您的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="@drawable/equalsymbol"
请参阅下面的第二个屏幕截图,我已经使用了app:srcCompat
,现在可以正常工作了
现在,一切正常。
因此,如上所述进行更改。 快乐编码
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.