android-layout 相关问题

布局定义用户界面的可视结构,例如活动,片段或应用程序窗口小部件的UI。

Android - TextInputEditText 文本拼写颠倒

我的片段中有一个 TextInputEditText,每当我输入文本时,它都会被倒转拼写。所以如果我尝试输入 Leonardo,它将被拼写为 odranoeL。光标位于...

回答 1 投票 0

Android TextView Marquee 不起作用

选框不适用于我的 TextView 请检查下面的代码 TextView 的 XML 代码 选框不适用于我的 TextView 请检查下面的代码 XML 代码 TextView <TextView android:id="@+id/mtextcash" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:layout_marginLeft="30dp" android:ellipsize="marquee" android:fadingEdge="horizontal" android:gravity="center" android:marqueeRepeatLimit="marquee_forever" android:maxLength="5" android:scrollHorizontally="true" android:scrollbars="horizontal" android:singleLine="true" android:text="Simple application that shows how to use marquee, with a long text" android:textColor="@color/white" android:textColorHint="@color/white" android:textSize="25dp" /> 在 Activity OnCreate 中 TextView inputAvailableCash = (TextView) findViewById(R.id.mtextcash); inputAvailableCash.setSelected(true); 提前致谢 由于文本很长,并且只有在单行中编写文本时您的代码才能工作。要么添加 android:singleline="true" 在 xml 中或将您的 java 代码更改为。 TextView inputAvailableCash = (TextView) findViewById(R.id.mtextcash); inputAvailableCash.setSelected(true); inputAvailableCash.setSingleLine(true); 这肯定对你有用。 一旦尝试将这些参数放入您的 TextView - 它就有效 android:singleLine="true" android:ellipsize="marquee" android:marqueeRepeatLimit="marquee_forever" android:scrollHorizontally="true" android:focusable="true" android:focusableInTouchMode="true" 而且你还需要setSelected(true): my_TextView.setSelected(true); 在 TextView 上运行选取框的最低代码是 <TextView . . android:singleLine="true" android:ellipsize="marquee" android:marqueeRepeatLimit="marquee_forever" /> 也不要忘记如下设置选择 textview.setSelected(true); 在xml文件中添加此属性 android:focusable="true" android:focusableInTouchMode="true" 对于选取框效果,宽度应始终为“match_parent”或静态(如 200dp...等)。并以编程方式将其设置为 setSelected true,就像您已经完成的那样,然后只需将其宽度设置为 xml 中的 match_parent 即可。 编辑的xml: <TextView android:id="@+id/mtextcash" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_centerVertical="true" android:layout_marginLeft="30dp" android:ellipsize="marquee" android:fadingEdge="horizontal" android:gravity="center" android:marqueeRepeatLimit="marquee_forever" android:maxLength="5" android:scrollHorizontally="true" android:scrollbars="horizontal" android:singleLine="true" android:text="Simple application that shows how to use marquee, with a long text" android:textColor="@color/white" android:textColorHint="@color/white" android:textSize="25dp" /> 如果通过执行 match_parent 它会影响您的设计,那么您必须通过固定其宽度或其他方式来管理它。 根据您使用的 xml 代码 android:maxLength="5" 意味着仅输入 5 个字符,因此您可以将其宽度固定为 50dp 或任何其他静态大小。 嘿,检查此代码,但是当您的文本大小(即长度较长)时,选框工作。它在我这边工作。:) <TextView android:id="@+id/mywidget" android:layout_width="wrap_content" android:layout_height="wrap_content" android:ellipsize="marquee" android:focusable="true" android:focusableInTouchMode="true" android:singleLine="true" android:textColor="#2086CA" android:text="Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum." android:textAppearance="?android:attr/textAppearanceSmall" /> 希望这有帮助。:) <TextView android:id="@+id/mtextcash" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="30dp" android:ellipsize="marquee" android:gravity="center" android:marqueeRepeatLimit="marquee_forever" android:scrollHorizontally="true" android:singleLine="true" android:text="Simple application that shows how to use marquee, with a long text" android:textColor="@color/white" android:textColorHint="@color/white" android:textSize="25dp" /> 我错误地设置了 android:maxLines="1" 而不是使用 android:singleLine="true"。切换后,一切都按预期运行。

回答 8 投票 0

调整 imageView 的大小会使其改变位置

我有一个相对布局,其中很少有图像,如下所示。 我有一个相对布局,其中很少有图像,如下所示。 <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" > <ImageView android:id="@+id/image_2" android:layout_width="30dp" android:layout_height="30dp" android:layout_alignLeft="@+id/increase" android:layout_alignParentTop="true" android:layout_marginLeft="34dp" android:layout_marginTop="34dp" android:src="@drawable/ic_launcher" /> <ImageView android:id="@+id/image_1" android:layout_width="30dp" android:layout_height="30dp" android:layout_alignBottom="@+id/image_2" android:layout_marginBottom="14dp" android:layout_toRightOf="@+id/increase" android:src="@drawable/ic_launcher" /> <ImageView android:id="@+id/image_8" android:layout_width="30dp" android:layout_height="30dp" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:src="@drawable/ic_launcher" /> </RelativeLayout> 在我的java文件中我需要执行一个小任务。 当我单击图像时,它的大小应该增加。 我尝试了这个,它有效,这是我的代码 public class MainActivity extends Activity implements OnClickListener { View imageView; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); ImageView i1 = (ImageView ) findViewById(R.id.image_1); ImageView i2 = (ImageView ) findViewById(R.id.image_2); ImageView i3 = (ImageView ) findViewById(R.id.image_8); i1.setOnClickListener(this); i2.setOnClickListener(this); i3.setOnClickListener(this); } public void onClick(View arg0) { Log.v("clicked ", "clicked"); imageView = arg0; Toast.makeText(this, "looking to resize the image ", 1).show(); Toast.makeText(this, "clicked 2", 1).show(); height = imageView.getHeight(); width = imageView.getWidth(); height += 50; width += 50; imageView.setLayoutParams(new RelativeLayout.LayoutParams(height, width)); } } 但我面临着一个问题。 当我单击图像时,它的位置正在改变。 考虑这些屏幕截图.. 以下是应用程序启动时的屏幕截图 下面是单击图像视图时的屏幕截图。 任何人都可以建议我,这样图像就不会改变其位置 我可以使用类似网格视图的东西(如果可能的话。但即使我试图在其中添加一些拖动选项。所以有人可以建议我吗) 更新代码: onclick 方法更新如下: public void onClick(View arg0) { Log.v("clicked ", "clicked"); imageView = arg0; Toast.makeText(this, "looking to resize the image ", 1).show(); Toast.makeText(this, "clicked 2", 1).show(); height = imageView.getHeight(); width = imageView.getWidth(); height += 50; width += 50; RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(height, width); params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, RelativeLayout.TRUE); params.setMargins( imageView.getLeft()-30,imageView.getTop()-30,imageView.getRight()+30, imageView.getBottom()+30); imageView.setLayoutParams(params); } 您为相对布局创建新的布局参数,并且仅设置高度和宽度。因此,图像视图将默认位于父相对布局的左上角。您还需要根据需要设置对齐方式和边距。 由于您使用的是相对布局,因此需要创建新的RelativeLayout布局参数: http://developer.android.com/reference/android/widget/RelativeLayout.LayoutParams.html 类似这样的: RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT); params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, RelativeLayout.TRUE); params.setMargins(left, top, right, bottom); 祝你好运...

回答 1 投票 0

如何让view下的view可点击?

我将回收器视图滚动到工具栏顶部,但工具栏项目(例如主页按钮)不可单击。 我的布局: 我将回收器视图滚动到工具栏顶部,但工具栏项目(例如主页按钮)不可单击。 我的布局: <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@color/primary"> <include android:id="@id/toolbar_feed" layout="@layout/toolbar_feed"/> // Inside viewPager is recycler view which scroll over the toolbar <android.support.v4.view.ViewPager android:id="@+id/view_pager" android:layout_width="match_parent" android:layout_height="match_parent" /> <include android:id="@+id/navigation_bottom_layout" layout="@layout/bottom_navigation_layout" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true"/> </RelativeLayout> 我唯一的问题是工具栏主页按钮不可单击,因为它位于 viewPager 层后面。它可以与播放translationZ一起使用,但仅支持API21+。那么,你们有办法让这项工作成功吗? 您可以使用 setOnTouchListener 方法来检测顶部视图上的触摸事件并将其传播到下面的视图。 topButton = findViewById(R.id.topButton); underneathButton = findViewById(R.id.underneathButton); topButton.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { // Propagate touch events to the view underneath underneathButton.dispatchTouchEvent(event); return true; } }); underneathButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // Handle click on the view underneath // Add your logic here } }); 更改在父视图中添加视图的顺序。 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@color/primary"> <android.support.v4.view.ViewPager android:id="@+id/view_pager" android:layout_width="match_parent" android:layout_height="match_parent"/> <include android:id="@id/toolbar_feed" layout="@layout/toolbar_feed"/> <include android:id="@+id/navigation_bottom_layout" layout="@layout/bottom_navigation_layout" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true"/> </RelativeLayout>

回答 2 投票 0

更改 CalendarView Android 中的字体系列

有没有办法在Android Studio的标准CalendarView中更改字体系列? 我尝试制作一种样式,但它仅适用于构建后的字体颜色。在预览窗口中,日历以我的字体显示-

回答 1 投票 0

如何让图标显示在 Android Material Button 中的文本旁边?

我正在尝试使用谷歌开发的材质按钮。由于第三方依赖,我无法使用 androidx 存储库,因此我当前正在使用 android 支持库(v28.0.0-alpha1,

回答 7 投票 0

ImageView/ImageButton 中的 srcCompat 和背景有什么不同

我在RelativeLayout中有一个ImageView。 ImageView 的背景是空的,srcCompat 链接到我的可绘制文件夹中的图像。在布局预览中,一切看起来都很好,但是......

回答 3 投票 0

如何将图像缩放到顶部中心? - 安卓

我有一个图像视图作为背景。它比屏幕大。我想将其水平居中。它应该与顶部对齐。因此,原始图像的顶部应该在屏幕上看到。我可以调整大小

回答 2 投票 0

RecyclerView 中 onBindView 上的 setAlpha() 在首次显示时不起作用

在 RecyclerView.Adapter 中使用以下代码: onBindViewHolder(VH 持有者, int 位置){ holder.itemView.setAlpha(0.5f); } 第一次显示该项目时不会显示 Alpha。哈...

回答 4 投票 0

大于屏幕宽度的LinearLayout不添加项目

首先,App.getInstance().getWidth() 给出了屏幕宽度。 我正在创建这个 LinearLayout: mainContainer = new LinearLayout(上下文); mainContainer.setLayoutParams(new LayoutParams(

回答 1 投票 0

如何在flutter应用程序中处理或重写Android设备后退按钮的操作?

我想将用户导航到主屏幕,并在按下另一个屏幕上的某个按钮后删除以前的导航堆栈。我暂时使用 Navigator.pushNamedAndRemoveUntil 方法...

回答 2 投票 0

以编程方式更改 TextInputLayout 聚焦和非聚焦提示颜色

我想在 TEXT INPUT LAYOUT 处于聚焦或未聚焦状态时更改相同的提示颜色。我有两个 TEXT INPUT LAYOUT 字段。两个字段的颜色相同。但它处于聚焦状态颜色

回答 4 投票 0

无法使用片段在一个 Activity 中显示两个 XML 布局

我的 Android 项目面临一个问题,我试图在一个 Activity 中显示两个 XML 布局并使用片段在它们之间切换。这是我的设置的细分: 1.活动结构...

回答 1 投票 0

系统底部导航栏与我的视图重叠

我遇到了一个问题,当使用三星手机(android 12)时,系统导航栏与我的视图重叠,如下所示 重叠导航栏 这是我的 style.xml 文件 <question vote="0"> <p>我遇到了一个问题,当使用三星手机(android 12)时,系统导航栏与我的视图重叠,如下所示 <a href="https://i.stack.imgur.com/HuSTa.png" target="_blank"><img src="https://cdn.txt58.com/i/AWkuc3RhY2suaW1ndXIuY29tL0h1U1RhLnBuZw==" alt="overlapping Nav bar "/></a></p> <p>这是我的 style.xml 文件</p> <pre><code>&lt;resources&gt; &lt;style name=&#34;AppTheme&#34; parent=&#34;Theme.MaterialComponents.Light.NoActionBar.Bridge&#34;&gt; &lt;item name=&#34;colorPrimary&#34;&gt;@color/colorPrimary&lt;/item&gt; &lt;item name=&#34;colorPrimaryDark&#34;&gt;@android:color/transparent&lt;/item&gt; &lt;item name=&#34;colorAccent&#34;&gt;@color/colorAccent&lt;/item&gt; &lt;item name=&#34;windowNoTitle&#34;&gt;true&lt;/item&gt; &lt;item name=&#34;android:windowTranslucentStatus&#34;&gt;true&lt;/item&gt; &lt;item name=&#34;android:windowTranslucentNavigation&#34;&gt;false&lt;/item&gt; &lt;item name=&#34;android:windowDrawsSystemBarBackgrounds&#34;&gt;false&lt;/item&gt; &lt;item name=&#34;android:windowBackground&#34;&gt;@drawable/shape_splash&lt;/item&gt; &lt;/style&gt; </code></pre> <p>我也尝试将 <pre><code>fitsSystemWindows</code></pre> 属性设置为 true,但它也不起作用。 即使尝试了 Activity_main.xml 文件中的 <pre><code>layout_marginBottom</code></pre> 参数,但仍然没有帮助。</p> <p>解决此问题的正确方法是什么?</p> </question> <answer tick="false" vote="0"> <p>添加您的主题.xml</p> <pre><code>&lt;item name=&#34;android:windowDrawsSystemBarBackgrounds&#34;&gt;true&lt;/item&gt; &lt;item name=&#34;android:windowTranslucentNavigation&#34;&gt;true&lt;/item&gt; </code></pre> <p>在Activity中setContentView(view)之前设置状态栏透明颜色</p> <pre><code> window.statusBarColor = Color.TRANSPARENT </code></pre> </answer> </body></html>

回答 0 投票 0

布局预览未在每个布局中显示 - Android Studio

一开始初始化时间比较长,初始化后预览也不起作用。每个布局都有同样的问题。我应该怎么做才能解决这个问题? 我已经尝试过使 Cac 无效...

回答 1 投票 0

arrayAdapter 中的资源类型问题

ArrayAdapter arrayAdapter = new ArrayAdapter<>(MainActivity.this, android.R.layout.simple_list_item_1, R.array.lsViewArray ); lsViewArray.XML: ArrayAdapter<String> arrayAdapter = new ArrayAdapter<>(MainActivity.this, android.R.layout.simple_list_item_1, R.array.lsViewArray ); lsViewArray.XML: <resources> <string-array name="lsViewArray"> <item>Magenta</item> <item>Green</item> <item>Blue</item> <item>Red</item> <item>Black</item> <item>Orange</item> <item>Purple</item> <item>Pink</item> <item>White</item> <item>Violet</item> <item>Saffron</item> <item>Yellow</item> <item>Brown</item> </string-array> </resources> 它给我带来了错误R.array.lsViewArray 错误:- 类型 id 的预期资源 除了在MainActivity中制作数组还能做什么 我想通过 lsViewArray.xml 访问它 或者我应该知道的其他方法 在当前代码中,您使用 R.array.lsViewArray 作为 textViewResourceId。 textViewResourceId 是要填充的布局资源中的 TextView 的 id。在您的情况下,要填充的布局资源是 android.R.layout.simple_list_item1。它不包含任何 ID 为 R.array.lsViewArray 的 Textview,因此您会收到错误。 要解决此问题,您可以使用接受 T[] 对象的构造函数。 首先,从您的资源中获取数组。为此,请输入: String[] lsViewArrayData = getResources().getStringArray(R.array.lsViewArray); 然后创建适配器并传递对象 lsViewArrayData: ArrayAdapter<String> arrayAdapter = new ArrayAdapter<>(MainActivity.this, android.R.layout.simple_list_item_1, lsViewArrayData);

回答 1 投票 0

Android 视图覆盖不阻止点击其后面的控件

嘿,我想弄清楚我做错了什么,因为这似乎并没有阻止视图覆盖层后面的控件。 这是我的 xml 布局代码: ...

回答 1 投票 0

键盘打开时背景图片向上滑动

我在 Android 中构建登录页面时遇到问题。我想将用户名和登录名保留在布局的底部。但是当输入用户名/密码时,键盘会弹出,还有我的背景图片

回答 3 投票 0

Jetpack Compose 中动画可绘制的替代方案是什么

我正在尝试在jetpack Compose中实现帧动画。 我知道在android视图系统中,可以通过使用AnimationDrawable来实现。 但是如何在jetp中正确使用动画drawable类...

回答 2 投票 0

Android - 在主屏幕上创建小部件

我想从url加载图像,以及图像下方显示的文本。像这样, ImageView 和 TextView 上的对齐,这是我的代码,但没有显示所需的布局 我想从网址加载图像,以及图像下方显示的文本。像这样, ImageView 和 TextView 上的对齐,这是我的代码,但没有显示所需的布局 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#00FFFF" android:padding="0.1dp"> <TextView android:text="@string/widgetUrl" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="0.8" android:layout_gravity="center_vertical" android:textColor="#000000"> </TextView> <TextView android:text="@string/widgetTitle" android:id="@+id/widgetTitle" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="0.8" android:layout_gravity="center_vertical" android:layout_alignParentBottom="true" android:layout_centerHorizontal="true" android:textColor="#ffffff"> </TextView> <ImageView android:id="@+id/widgetBackground" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="0.5" android:src="@drawable/ic_launcher" android:layout_gravity="center_vertical"> </ImageView> </LinearLayout> 如何从网络加载图像并在java代码中显示在布局上?以下是我的小部件提供程序代码: @Override public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { super.onUpdate(context, appWidgetManager, appWidgetIds); Log.i(WIDGETTAG, "onUpdate"); final int N = appWidgetIds.length; // Perform this loop procedure for each App Widget that belongs to this provider for (int i=0; i<N; i++) { int appWidgetId = appWidgetIds[i]; Log.i(WIDGETTAG, "updating widget[id] " + appWidgetId); RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget_layout); /* View setup */ views.setInt(R.id.widgetTitle, "setBackgroundColor", Color.argb(150, 0, 0, 0)); Intent intent = new Intent(context, ChopInkService.class); intent.setAction(ChopInkService.UPDATE_IMAGE); intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId); PendingIntent pendingIntent = PendingIntent.getService(context, 0, intent, 0); views.setOnClickPendingIntent(R.id.widgetBackground, pendingIntent); Log.i(WIDGETTAG, "pending intent set"); // Tell the AppWidgetManager to perform an update on the current App Widget appWidgetManager.updateAppWidget(appWidgetId, views); } Service和AppWidgetProvider的工作是什么? 从 url 加载图像是 service 或 AppWidgetProvider 的工作? 视图设置应该放在service或AppWidgetProvider? 当用户点击小部件时,如何将用户重定向到 Play 商店? 提前致谢。我是新手,如果我问了一个愚蠢的问题,请原谅。 来自 Lars Vogel 的 Android(主屏幕)小部件 - 教程 (vogella.com): 可用的小部件视图和布局 widget 仅限于它可以使用的 View 类。作为布局 您可以使用 FrameLayout、LinearLayout 和 RelativeLayout 类。作为视图,您可以使用 AnalogClock、Button、Chromometer、 ImageButton、ImageView、ProgressBar 和 TextView。 从 Android 3.0 开始,提供了更多视图:GridView、ListView、 StackView、ViewFlipper 和 AdapterViewFlipper。 此适配器视图 要求您定义一个描述的集合视图小部件 在本教程的后面部分。 小部件视图上唯一可能的交互是通过 在 OnClickListener 上。此 OnClickListener 可以注册在小部件上 并且由用户触发。 AppWidgetProvider 您的 BroadcastReceiver 通常会扩展 AppWidgetProvider 类。 AppWidgetProvider 类实现了 onReceive() 方法, 提取所需的信息并调用以下小部件 生命周期方法。 由于您可以将小部件的多个实例添加到主屏幕,因此您可以 具有仅在第一个实例中调用的生命周期方法 添加/删除到主屏幕和其他需要的内容 您的小部件的每个实例。 Widget 的生命周期 onEnabled() - 首次将小部件实例添加到主屏幕时调用 onDisabled() - 从主屏幕中删除小部件的最后一个实例后调用。 onUpdate() - 每次更新小部件时调用。包含需要更新的appWidgetIds的id。请注意,这可能 是该提供程序的所有 AppWidget 实例,或者只是一个子集 其中,如 JavaDoc 方法中所述。例如,如果超过 主屏幕上添加了一个小部件,仅最后一个发生变化 (直到重新安装)。 onDeleted() -Widget 实例从主屏幕中删除 这些方法中所有长时间运行的操作都应该在一个 服务,因为广播接收器的执行时间是有限的。 在 onReceive() 方法中使用异步处理没有帮助 因为系统可以在 onReceive() 之后终止广播进程 方法。 有关小部件的更多详细信息请检查如何在Android中创建小部件? 教程1 教程2 嗨,您可以查看这个教程,了解从 URL (Http) 加载图像。 尝试以下代码: URL 图像小部件提供程序类 public class URLImageAppWidgetProvider extends AppWidgetProvider { public static String TAG = "URLImageWidget"; public static class Size_1_1 extends URLImageAppWidgetProvider {} public static class Size_1_2 extends URLImageAppWidgetProvider {} public static class Size_1_4 extends URLImageAppWidgetProvider {} public static class Size_2_2 extends URLImageAppWidgetProvider {} @Override public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { super.onUpdate(context, appWidgetManager, appWidgetIds); SharedPreferences urls = context.getSharedPreferences("urls.conf", Context.MODE_PRIVATE); for (int id : appWidgetIds) { String url = urls.getString("url_" + id, ""); update(context, appWidgetManager, id, url); } } @Override public void onDeleted(Context context, int[] appWidgetIds) { super.onDeleted(context, appWidgetIds); SharedPreferences urls = context.getSharedPreferences("urls.conf", Context.MODE_PRIVATE); SharedPreferences.Editor urls_editor = urls.edit(); for (int id : appWidgetIds) { urls_editor.remove("url_" + id); } urls_editor.commit(); } public static void update(final Context context, final AppWidgetManager appWidgetManager, final int id, final String url) { new Thread() { public void run() { Bitmap img = getBitmapFromUrl(url); if (img != null) { RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.main); views.setImageViewBitmap(R.id.img, img); appWidgetManager.updateAppWidget(id, views); } } }.start(); } private static Bitmap getBitmapFromUrl(final String url) { try { return BitmapFactory.decodeStream(((java.io.InputStream)new java.net.URL(url).getContent())); } catch (Exception e) { return null; } } } 这里是 URL Image 小部件配置类 public class URLImageAppWidgetConfiguration extends Activity { private int id; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.configuration); setResult(RESULT_CANCELED); Intent intent = getIntent(); Bundle extras = intent.getExtras(); if (extras != null) { id = extras.getInt( AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID); } if (id == AppWidgetManager.INVALID_APPWIDGET_ID) { finish(); } } public void addWidget(View v) { SharedPreferences urls = getSharedPreferences("urls.conf", Context.MODE_PRIVATE); SharedPreferences.Editor urls_editor = urls.edit(); String url = ((TextView) findViewById(R.id.url)).getText().toString(); if (!url.startsWith("http://")) url = "http://" + url; urls_editor.putString("url_" + id, url); urls_editor.commit(); AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(this); URLImageAppWidgetProvider.update(this, appWidgetManager, id, url); setResult(RESULT_OK, new Intent().putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, id) ); finish(); } }

回答 2 投票 0

© www.soinside.com 2019 - 2024. All rights reserved.