Android-如何在 LinearLayout 中将我的自定义 View 居中?

标签 android layout center

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center_horizontal">

    <com.example.root.howold.MyRing
        android:layout_centerInParent="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>

我也尝试设置gravitylayout_gravity,但它们实际上不起作用。

如何在布局中使 MyRing(我的自定义 View )居中

最佳答案

Try this:-

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <com.example.root.howold.MyRing
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true" />

</RelativeLayout>

关于Android-如何在 LinearLayout 中将我的自定义 View 居中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31869660/

相关文章:

java - 为什么我的 SQLiteOpenHelper 构造函数中出现 "incompatible types: Bla cannot be converted to Context"?

android - Google NFC API 和 Open NFC API 之间的区别?

android - 操作栏菜单项onclick?

android - 如何在不丢失纵横比的情况下缩放方形图像(即使布局不是方形的)

java - GridBagLayout 网格化不起作用

ios:以编程方式要求 Game Center 登录?

python - 如何居中对齐 numpy.savetxt 生成的文本?

css - 垂直对齐图像居中 :middle in CSS not working

android - <include> 标签上的 ID 破坏了布局

html - CSS-Grid:如何在不缩小元素本身的情况下将内容居中?