GVKun编程网logo

使用 imageEdgeInsets 和 titleEdgeInsets 对齐 UIButton 上的文本和图像

9

此处将为大家介绍关于使用imageEdgeInsets和titleEdgeInsets对齐UIButton上的文本和图像的详细内容,此外,我们还将为您介绍关于AndroidStudio:尝试在空对象引

此处将为大家介绍关于使用 imageEdgeInsets 和 titleEdgeInsets 对齐 UIButton 上的文本和图像的详细内容,此外,我们还将为您介绍关于Android Studio:尝试在空对象引用上调用虚拟方法“void android.widget.ImageButton.setImageResource(int)”、android.support.design.widget.CheckableImageButton的实例源码、Android:在 Button 或 ImageButton 上组合文本和图像、Android:在Button或ImageButton上组合文本和图像的有用信息。

本文目录一览:

使用 imageEdgeInsets 和 titleEdgeInsets 对齐 UIButton 上的文本和图像

使用 imageEdgeInsets 和 titleEdgeInsets 对齐 UIButton 上的文本和图像

我想在两行文本的左侧放置一个图标,使图像和文本开头之间有大约 2-3 像素的空间。控件本身水平居中对齐(通过 Interface Builder 设置)

该按钮将类似于以下内容:

|                  |
|[Image] Add To    |
|        Favorites |

我正在尝试使用 contentEdgeInset、imageEdgeInsets 和 titleEdgeInsets
进行配置,但无济于事。我知道负值会扩大边缘,而正值会缩小边缘以使其更靠近中心。

我试过:

[button setTitleEdgeInsets:UIEdgeInsetsMake(0,-image.size.width,0)];
[button setImageEdgeInsets:UIEdgeInsetsMake(0,button.titleLabel.bounds.size.width,0)];

但这不能正确显示。我一直在调整这些值,但是从左侧插入值的 -5 到 -10 似乎并没有以预期的方式移动它。-10 会将文本一直向左移动,所以我希望 -5
将它从左侧移动到一半,但事实并非如此。

插图背后的逻辑是什么?我不熟悉图像放置和相关术语。

Android Studio:尝试在空对象引用上调用虚拟方法“void android.widget.ImageButton.setImageResource(int)”

Android Studio:尝试在空对象引用上调用虚拟方法“void android.widget.ImageButton.setImageResource(int)”

您的 post_item.xml 文件是否附加到 Recycler 视图?如果是,则必须使用点击侦听器。

,

只需从 recyclerAdapter.java

访问您的按钮

android.support.design.widget.CheckableImageButton的实例源码

android.support.design.widget.CheckableImageButton的实例源码

项目:material-components-android    文件:TextInputLayoutMatchers.java   
/** Returns a matcher that matches TextInputLayouts with non-displayed password toggles */
public static Matcher<View> passwordToggleIsNotChecked() {
  return new TypeSafeMatcher<View>(TextInputLayout.class) {
    @Override
    public void describeto(Description description) {
      description.appendText("TextInputLayout has checked password toggle.");
    }

    @Override
    protected boolean matchesSafely(View item) {
      // Reach in and find the password toggle since we don't have a public API
      // to get a reference to it
      CheckableImageButton passwordToggle = item.findViewById(R.id.text_input_password_toggle);
      return !passwordToggle.isChecked();
    }
  };
}
项目:Flashcards    文件:LoginActivity.java   
public void setupUI(View view) {

        // Set up touch listener for non-text Box views to hide keyboard.
        // We should also ignore "show password" icon
        if (!(view instanceof EditText) && !(view instanceof CheckableImageButton)) {
            view.setonTouchListener((v,event) -> {
                hideKeyboard();
                return false;
            });
        }

        //If a layout container,iterate over children and seed recursion.
        if (view instanceof ViewGroup) {
            for (int i = 0; i < ((ViewGroup) view).getChildCount(); i++) {
                View innerView = ((ViewGroup) view).getChildAt(i);
                setupUI(innerView);
            }
        }
    }
项目:dhis2-android-dashboard    文件:MapImageViewFragment.java   
@Override
public void onViewCreated(View view,@Nullable Bundle savedInstanceState) {
    viewBaseMapButton =
            (CheckableImageButton) view.findViewById(R.id.view_base_map_button);


    viewBaseMapButton.setonClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            modeWithBaseMap = !modeWithBaseMap;

            showMapImage(modeWithBaseMap);
        }
    });


    showMapImage(modeWithBaseMap);
}
项目:dhis2-android-dashboard    文件:InterpretationAdapter.java   
private IInterpretationViewHolder onCreateContentViewHolder(ViewGroup parent,int viewType) {
    switch (viewType) {
        case ITEM_WITH_IMAGE_TYPE: {
            View rootView = getLayoutInflater().inflate(
                    R.layout.recycler_view_dashboard_item_imageview,parent,false);

            ImageView imageView = (ImageView) rootView.findViewById(R.id.dashboard_item_image);


            CheckableImageButton modeButton = (CheckableImageButton)
                    rootView.findViewById(R.id.view_mode_button);

            return new ImageItemViewHolder(rootView,modeButton,imageView,mClickListener);
        }
        case ITEM_WITH_TABLE_TYPE: {
            TextView textView = (TextView) getLayoutInflater()
                    .inflate(R.layout.recycler_view_dashboard_item_textview,false);
            return new TextItemViewHolder(textView,mClickListener);
        }
    }
    return null;
}
项目:dhis2-android-dashboard    文件:InterpretationAdapter.java   
public ImageItemViewHolder(View rootView,CheckableImageButton modeButton,ImageView view,OnItemClickListener outerListener) {
    this.rootView = rootView;
    this.imageView = view;
    this.modeButton = modeButton;

    mImageLoader = PicassoProvider.getInstance(rootView.getContext(),false);

    listener = new OnInterpretationInternalClickListener(outerListener);
    imageView.setonClickListener(listener);

    modeButton.setonClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            modeWithBaseMap = !modeWithBaseMap;

            showMapImage(modeWithBaseMap);
        }
    });
}
项目:dhis2-android-dashboard    文件:DashboardItemAdapter.java   
public ImageItemViewHolder(View rootView,false);

    listener = new OnElementInternalClickListener(outerListener);
    imageView.setonClickListener(listener);

    modeButton.setonClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            modeWithBaseMap = !modeWithBaseMap;

            showMapImage(modeWithBaseMap);
        }
    });
}
项目:dhis2-android-dashboard    文件:DashboardItemAdapter.java   
/**
 * Depending on viewType,this method will return correct IElementContentViewHolder.
 *
 *
 * @param rootView
 * @param parent   Parent ViewGroup.
 * @param viewType Type of view we want to get IElementContentViewHolder for.
 * @return view holder.
 */
private IElementContentViewHolder onCreateElementContentViewHolder(
        ViewGroup parent,mClickListener);
        }
        case ITEM_WITH_LIST_TYPE: {
            LinearLayout textViewContainer = (LinearLayout) getLayoutInflater()
                    .inflate(R.layout.recycler_view_dashboard_item_list,false);
            return new ListItemViewHolder(textViewContainer,mClickListener,mDashboardAccess);
        }
    }
    return null;
}
项目:africastalking-android    文件:CardTextInputLayout.java   
public CheckableImageButton getpasswordToggleView() {
    return mPasswordToggleView;
}
项目:africastalking-android    文件:PinTextInputLayout.java   
public CheckableImageButton getpasswordToggleView() {
    return mPasswordToggleView;
}
项目:Luhn    文件:CardTextInputLayout.java   
public CheckableImageButton getpasswordToggleView() {
    return mPasswordToggleView;
}
项目:Luhn    文件:PinTextInputLayout.java   
public CheckableImageButton getpasswordToggleView() {
    return mPasswordToggleView;
}

Android:在 Button 或 ImageButton 上组合文本和图像

Android:在 Button 或 ImageButton 上组合文本和图像

我正在尝试在按钮上添加图像(作为背景)并根据运行时发生的情况动态添加图像上方/上方的一些文本。

如果我使用ImageButton,我什至无法添加文本。如果我使用Button,我可以添加文本,但只定义具有此处android:drawableBottom定义的类似
XML
属性的图像。

然而,这些属性只结合了 x 和 y 维度的文本和图像,这意味着我可以在文本周围绘制图像,但不能在文本下方/下方(z 轴定义为从显示器中出来)。

关于如何做到这一点的任何建议?一个想法是扩展ButtonImageButton覆盖draw()-
方法。但是以我目前的知识水平,我真的不知道如何做到这一点(2D 渲染)。也许有更多经验的人知道解决方案或至少有一些开始的指示?

答案1

小编典典

您可以调用setBackground()aButton来设置按钮的背景。

任何文本都将出现在背景之上。

如果您在 xml 中寻找类似的东西,则有: android:background以相同方式工作的属性。

Android:在Button或ImageButton上组合文本和图像

Android:在Button或ImageButton上组合文本和图像

我正在尝试在按钮上创建一个图像(作为背景)并动态添加,具体取决于运行时发生的情况,图像上方/上方的一些文本。

如果我使用ImageButton我甚至无法添加文本。 如果我使用Button我可以添加文本,但只定义一个带有android:drawableBottom的图像和此处定义的类似XML属性。

然而,这些属性仅在x和y维度中组合文本和图像,这意味着我可以在文本周围绘制图像,但不能在文本的下方/下方(z轴定义为从显示中出来)。

有关如何做到这一点的任何建议? 一个想法是扩展ButtonImageButton并覆盖draw() - 方法。 但凭借我目前的知识水平,我真的不知道如何做到这一点(2D渲染)。 也许有经验的人知道解决方案或至少有一些指针可以开始?


#1楼

您可以在Button上调用setBackground()来设置Button的背景。

任何文本都将显示在背景上方。

如果你在xml中寻找类似的东西,那么: android:background属性的工作方式相同。


#2楼

<Button
     android:id="@+id/groups_button_bg"
     android:layout_height="wrap_content"
     android:layout_width="wrap_content"
     android:text="Groups"
     android:drawableTop="@drawable/[image]" />


android:drawableLeft
android:drawableRight
android:drawableBottom
android:drawableTop

http://www.mokasocial.com/2010/04/create-a-button-with-an-image-and-text-android/


#3楼

你可以用这个:

  <Button
                    android:id="@+id/reset_all"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginRight="5dp"
                    android:layout_weight="1"
                    android:background="@drawable/btn_med"
                    android:text="Reset all"
                    android:textColor="#ffffff" />

                <Button
                    android:id="@+id/undo"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="5dp"
                    android:layout_weight="1"
                    android:background="@drawable/btn_med"
                    android:text="Undo"
                    android:textColor="#ffffff" />

因为我把图像作为background ,并添加了文字..!


#4楼

您可以使用drawableTop (也是drawableLeft等)为图像添加gravity left|center_vertical

<Button
            android:id="@+id/btn_video"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:background="@null"
            android:drawableTop="@drawable/videos"
            android:gravity="left|center_vertical"
            android:onClick="onClickFragment"
            android:text="Videos"
            android:textColor="@color/white" />

#5楼

这段代码非常适合我

<LinearLayout
    android:id="@+id/choosePhotosView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:gravity="center"
    android:clickable="true"
    android:background="@drawable/transparent_button_bg_rev_selector">

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/choose_photo"/>

     <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="@android:color/white"
        android:text="@string/choose_photos_tv"/>

</LinearLayout>

今天的关于使用 imageEdgeInsets 和 titleEdgeInsets 对齐 UIButton 上的文本和图像的分享已经结束,谢谢您的关注,如果想了解更多关于Android Studio:尝试在空对象引用上调用虚拟方法“void android.widget.ImageButton.setImageResource(int)”、android.support.design.widget.CheckableImageButton的实例源码、Android:在 Button 或 ImageButton 上组合文本和图像、Android:在Button或ImageButton上组合文本和图像的相关知识,请在本站进行查询。

本文标签: