我有一个图像ImageButton,在mdpi(中等dpi)屏幕上看起来不错.但是,当设备具有hdpi(高dpi)屏幕时,图像仍然是逐像素精确的,在设备屏幕上看起来很小.
如何使图像(以及因此ImageButton)基于与密度无关的像素(dp或dip)而不是像素进行缩放?
我正在尝试使ImageButton处于压缩,聚焦和正常的状态.我在这里阅读了ImageButton的介绍
我已将选择器XML文件保存在/ res/drawable /中.还设置了android:src="@drawable/my_selector".
my_selector.xml文件:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/btn_unfocus" /> <!-- default -->
<item android:state_pressed="true"
android:drawable="@drawable/btn_onpress" /> <!-- pressed -->
<item android:state_focused="true"
android:drawable="@drawable/btn_onfocus" /> <!-- focused -->
</selector>
Run Code Online (Sandbox Code Playgroud)
我想我已经完成了正确的ImageButton.但它对我不起作用.
这有什么不对?
请指教.
编辑:发布完整的代码(除了XML,因为它是一堆荒谬的表格格式!)请忽略与我的问题无关的代码!我现在正在提供功能.我稍后会清理它.
大家好!第一个应用和第一个问题.我在这里研究了一段时间,通常会找到我的答案,但我有一个可能非常明显的bugger.我有一个imageButton似乎没有调用指定的方法.
我的imageButton的XML:
<ImageButton
android:background="@null"
android:onClick="click"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageButton1"
android:src="@drawable/stats"
android:layout_gravity="center_vertical">
</ImageButton>
Run Code Online (Sandbox Code Playgroud)
我的代码:
package com.talismancs;
import android.app.Activity;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
public class sheet extends Activity{
private String selection;
private String pick;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.sheet);
Bundle extras = getIntent().getExtras();
if(extras !=null) {
// Get extra from .main and remove spaces
String pick = extras.getString(selection);
pick = pick.replace(" ", …Run Code Online (Sandbox Code Playgroud) 我想知道是否有一种方法可以在单击时更改按钮图像.最初,按钮有一个暂停图标.单击它时,我希望显示播放图标.每次单击该按钮时,图标应在播放和暂停之间变化.
有没有办法做到这一点?
XML布局文件:
<ImageButton
android:id="@+id/play"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="109dp"
android:src="@drawable/play_btn"
android:background="@null" />
Run Code Online (Sandbox Code Playgroud)
play_btn.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/pause" android:state_selected="true" />
<item android:drawable="@drawable/play" />
</selector>
Run Code Online (Sandbox Code Playgroud) 如何创建没有边框的ImageButton(只是图像应该是可见的)?人们可以通过设置来实现这一点imageButton.setBackgroundDrawable(null),但这也会消除焦点和选择颜色.
目标是最初只能看到没有边框的图像.但是当用户聚焦/触摸/点击图像时,应该通过像常规按钮一样高亮显示图像来指示.
用于API 14的Java-Code中的解决方案是优选的.谢谢!
如何防止ImageButton拉伸图像?
使用此代码段:
<ImageButton
android:src="@drawable/prediction"
android:background="?android:attr/selectableItemBackground"
android:gravity="center_horizontal"
android:scaleType="centerInside"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:contentDescription="some description"
android:text="@string/predict"
local:MvxBind="Click ExecuteQueryCmd" />
Run Code Online (Sandbox Code Playgroud)
我得到以下效果:

图像是64x64 png.
我想要的只是让ImageButton尊重它所分配的图像的原始宽度和长度,从而分辨率.基于类似帖子的建议是使用Button,将其背景设置为图像然后使用自定义选择器,但这听起来像一个黑客.或者这是正确的方法吗?
我正在使用Imagebuttons在我的android项目中显示一些图标.
<ImageButton
android:id="@+id/button_one"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:padding="20dp"
android:contentDescription="@string/button_one"
android:background="@android:color/transparent"
android:src="@drawable/button_one" />
Run Code Online (Sandbox Code Playgroud)
我想,当按钮处于按下状态时,会出现一个带圆角的白色边框.我该怎么做才能实现这一目标?这可以仅使用代码完成,还是需要额外的背景图像?
我正在尝试转换布局,以便它包含片段.其中一个视图是具有侦听器的ImageButton.代码作为一个活动工作正常,但作为一个片段麻烦...
第一个问题是我无法使用findViewById,但我能够在这里找到答案并用getView()修复它.
在我宣布之后,我无法找到第二个问题的答案:
ImageButton camBt = (ImageButton)getView().findViewById(R.id.button1);
Run Code Online (Sandbox Code Playgroud)
我想设置一个on click监听器:
camBt.setOnClickListener(listener);
Run Code Online (Sandbox Code Playgroud)
但它一直表现得像ImageButton(camBt)不存在......我甚至不知道问题是什么......它在Activity中工作得很好......
这是完整的代码.非常感谢!
public class CameraFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View V = inflater.inflate(R.layout.camera_fragment, container, false);
return V;
}
ImageButton camBt = (ImageButton)getView().findViewById(R.id.button1);
camBt.setOnClickListener(listener);
ImageButton.OnClickListener listener = new ImageButton.OnClickListener()
{
@Override
public void onClick(View arg0)
{
Camera.open().getParameters();
}
};
}
Run Code Online (Sandbox Code Playgroud) android listener imagebutton android-fragments onclicklistener
我使用在线教程创建了一个简单的应用程序,它将ImageButton添加到窗口中.窗口我的意思是ImageButton显示在其余活动之上.它始终可见.
我不知道如何处理聊天头的点击事件.我设置了一个onClickListener但它没有做任何事情.
这是我的ChatHeadService.java
package com.example.test;
import android.app.Service;
import android.content.Intent;
import android.graphics.PixelFormat;
import android.os.IBinder;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.widget.ImageButton;
import android.widget.ImageView;
public class ChatHeadService extends Service {
private WindowManager windowManager;
private ImageView chatHead;
@Override
public IBinder onBind(Intent intent) {
// Not used
return null;
}
@Override
public void onCreate() {
super.onCreate();
windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
chatHead = new ImageButton(this);
chatHead.setImageResource(R.drawable.ic_launcher);
final WindowManager.LayoutParams params = new WindowManager.LayoutParams(
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.TYPE_PHONE,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
PixelFormat.TRANSLUCENT);
params.gravity = Gravity.CENTER; …Run Code Online (Sandbox Code Playgroud) 下午好,我正在尝试使用阴影创建带有ImageButton的图像.
要做到这一点 :
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:padding="5sp">
<ImageButton
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/circle_shape_little"
android:src="@drawable/ic_keyboard_arrow_right_black_24dp"
android:elevation="3sp"/>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
但结果如下:
如你所见,边界是"切",我不知道为什么.
有人能帮帮我吗?谢谢.
android ×10
imagebutton ×10
android-xml ×1
dpi ×1
elevation ×1
listener ×1
onclick ×1
overlay ×1
xml ×1