android-layout 相关问题

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

调整 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

约束布局中带有 Textview 的动态按钮

目前我正在使用(学习使用)ConstraintLayout。现在我创建了一个简单的布局,每个按钮下每行有 3 个按钮,我放置了一个文本视图,目前我正在手动添加按钮...

回答 1 投票 0

Android 键盘隐藏 EditText

当我尝试在屏幕底部的 EditText 中写入内容时,软键盘会隐藏 EditText。我该如何解决这个问题?下面是我的 xml 代码。我在 Frag 中使用这个...

回答 12 投票 0

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