宽度和高度相等的视图

Ian*_*Ian 1 android android-layout

我想在视图中做的是有一个textview,在它的右边,有另一个自定义视图X,其高度与textview相同,并且width = height:

+---------------------+---+
|A flexible string    | X |
+---------------------+---+
Run Code Online (Sandbox Code Playgroud)

我到目前为止的尝试是:

里面的X类:

@Override
protected void onMeasure (int widthMeasureSpec, int heightMeasureSpec) {
    int height = getDefaultSize(getSuggestedMinimumHeight(), heightMeasureSpec);
    setMeasuredDimension(height,height);
    Log.v("measure","width:"+height + " height:"+height);
}
Run Code Online (Sandbox Code Playgroud)

和我的xml:

<RelativeLayout
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content">

    <TextView android:id="@+id/mytext"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:layout_alignParentLeft="true"/>

    <mystuff.X
     android:layout_width="wrap_content"
     android:layout_height="fill_parent"
     android:layout_toRightOf="@id/mytext"/>

</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)

日志语句打印出来:

width:1073741823 height:1073741823
width:37 height:37
width:1073741823 height:1073741823
width:37 height:37
width:1073741823 height:1073741823
width:37 height:37
Run Code Online (Sandbox Code Playgroud)

创建视图并填充屏幕宽度时:

+---------------------+-----------------+
|A flexible string    |      X          |
+---------------------+-----------------+
Run Code Online (Sandbox Code Playgroud)

我非常感谢任何帮助.谢谢!伊恩

Saj*_*jid 5

您将度量规格视为实际像素.它们实际上是代表"fill_parent","wrap_content"等的整数.

你想要做的是:

@Override
protected void onMeasure (int widthMeasureSpec, int heightMeasureSpec) {
    int height = getDefaultSize(getSuggestedMinimumHeight(), heightMeasureSpec);
    setMeasuredDimension(height,height);
    Log.v("measure","width:"+height + " height:"+height);
}
Run Code Online (Sandbox Code Playgroud)