简体   繁体   English

如何在拥有图像时从图像到textView删除边距?

[英]How do i remove margin from image to a textView when having?

I got a few images and textViews in a LinearLayout. 我在LinearLayout中得到了一些图像和textViews。 The LinearLayout is having margin top and margin bottom 8sp or so. LinearLayout的边距顶部和底部边距为8sp左右。

Problem is all my images are getting that margin aswell even if i tell them to have margin 0 the same way i told my LinearLayout to have margin 0 问题是,即使我告诉它们所有图像的边距都为0,就像我告诉LinearLayout的边距为0一样

在人像模式下,Android图片会获得边距在横向上,图像和文本之间的空白消失 As you see, the margin dissapear when i turn the screan to landscape mode. 如您所见,当我将screan转到横向模式时,边距消失。

What have i done wrong? 我做错了什么?

Everything is done programmatically ie no XML files is used. 一切都以编程方式完成,即不使用XML文件。 Here is how i set margin on my LinearLayout 这是我在LinearLayout上设置边距的方法

LinearLayout contentLayout = new LinearLayout(getActivity());
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
    LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params.setMargins(0,8,0,8);
contentLayout.setLayoutParams(params);

Solved by using android:adjustViewBounds on the image 通过在图像上使用android:adjustViewBounds解决

尝试将方向设置为垂直

contentLayout.setOrientation(1);

Try this: 尝试这个:

//obtain dp equivalent of pixels
final float scale = getResources().getDisplayMetrics().density;
int margin = (int) (5 * scale + 0.5f);

Apply to your layout params. 应用于布局参数。

//setMargins(left, top, right, bottom)
params.setMargins(margin,margin,margin,margin);

I think you are not setting marginLeft 我认为您没有设置marginLeft

Let me know if that helps! 让我知道是否有帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从DialogFragment中的textView中设置Text() - How do I setText() in an textView from a DialogFragment 如何将带有Imageview和TextView的RelativeLayout转换为PNG图像? - How do I convert a RelativeLayout with an Imageview and TextView to a PNG image? 按Enter键或按钮时,如何将EditText中的文本打印到textview - How do I print text from EditText to a textview when pressed enter or a button Android studio:从数据库中删除项目时,如何更新回收站视图中的文本视图? - Android studio: How do I update a textview in a recyclerview when an item from the database is deleted? 如何从LinkedHashMap中删除空值 <String,Object> 嵌套对象具有空值 - How do I remove null values from LinkedHashMap<String,Object> with nested objects having null values 如何动态设置textview的边距? - How to set the margin of a textview dynamiclly? 单击按钮时,如何通过编程方式创建一个新的TextView? - How do I create a New TextView Progmatically when a button is clicked? 如何预览 TextView? - How do i preview a TextView? 如何仅使用单个图像使用OpenCV从Java中的图像中删除背景? - How do I remove the background from image in Java using OpenCV with only single Image? 如何从其他类更新TextView? - How do I update a TextView from a different class?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM