【问题标题】:Set text gravity center in square Frame layout在方形框架布局中设置文本重心
【发布时间】:2017-03-06 11:39:36
【问题描述】:

我需要在 center FrameLayout 中设置文本,但如果我 setGravity center 它将是 center 只有 horizo​​ntal 。 我创建了方形框架布局。

public class SquareFrameLayout extends FrameLayout {
public SquareFrameLayout(@NonNull Context context) {
    super(context);
}

public SquareFrameLayout(@NonNull Context context, @Nullable AttributeSet attrs) {
    super(context, attrs);
}

public SquareFrameLayout(@NonNull Context context, @Nullable AttributeSet attrs, @AttrRes int defStyleAttr) {
    super(context, attrs, defStyleAttr);
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    int width = MeasureSpec.getSize(widthMeasureSpec);
    int height = MeasureSpec.getSize(heightMeasureSpec);
    int size = width < height ? height:width;
    setMeasuredDimension(size,size);
}

<com.example.student.game.SquareFrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/GameButtonFrameLayout"
    android:background="#beb7b7"
    android:layout_margin="15dp"
    android:clickable="true">
<TextView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/GameButtonText" />
<ImageView
    android:id="@+id/GameButtonImage"
    android:layout_gravity="center"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>
</com.example.student.game.SquareFrameLayout>    

但如果我将重力 - Gravity.CENTER 设置为 textView(位于 SquareFrameLayout),重力只会设置中心水平

【问题讨论】:

  • 你能把你的xml代码也给我看看吗?
  • 我无法确切知道,因为我们看不到您的 xml,但过去我发现我的高度设置为 wrap_content 而不是 match_parent。我希望这会有所帮助
  • 我添加了XML,高度设置为match_parent。
  • 也许可以尝试:android:layout_centerInParent="true",但将高度和宽度设置为 wrap_content

标签: android android-studio android-framelayout


【解决方案1】:

在文本视图中添加重心。 希望这会有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-06-09
    • 2018-03-01
    • 1970-01-01
    • 2023-04-08
    • 1970-01-01
    • 2020-01-13
    • 2020-05-11
    相关资源
    最近更新 更多