【问题标题】:getLeft() not working for TextView in androidgetLeft()不适用于Android中的TextView
【发布时间】:2014-10-29 01:41:32
【问题描述】:

我在 android 中创建了一个自定义控件(按钮),它有一个左图像和中心文本以及右箭头图像。在使用这个自定义控件时,我必须为这个按钮绘制上边框和下边框。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white"
android:orientation="vertical" >

<View
         android:id="@+id/viwupperline"
         style="@style/bsf_viwline"/> 
<RelativeLayout
    android:id="@+id/lytcontrollayout"
    android:layout_width="fill_parent"
    android:paddingLeft="12dp"
    android:paddingRight="12dp"
    android:layout_height="38dip"
    android:layout_margin="5dip" >

    <ImageView
        android:id="@+id/imgrightimage"
        android:layout_width="25dip"
        android:layout_height="25dip"
        android:layout_centerVertical="true"
       />

    <TextView
        android:id="@+id/txttextoncontrol"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingLeft="5dp"
        android:layout_centerVertical="true"
        android:layout_toRightOf="@+id/imgrightimage"
        android:textSize="17sp"
        android:textColor="@color/grey" >
    </TextView>

    <ImageView
        android:id="@+id/imgarrow"
        android:layout_width="20dip"
        android:layout_height="20dip"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:src="@drawable/bsfarrowright" >
    </ImageView>

</RelativeLayout>

    <View
         android:id="@+id/viwbottomline"
         style="@style/bsf_viwline"/>

如果我只使用一个按钮,则必须显示完整的上边框和下边框。否则底部边框应与 TextView 对齐绘制。我为边界创建了视图。我将 getLeft() 用于 TextView。如果底部边框应该最小化,我为视图设置 marginleft 如下:

public void AddMarginToLowerBorder()
{
    int margin = _text.getLeft();
    LinearLayout.LayoutParams params = (LinearLayout.LayoutParams)_viewlower.getLayoutParams();
    params.setMargins(margin, 0, 0, 0);
    _viewlower.setLayoutParams(params);
}

但是 getLeft() 不起作用。

如果我这样设置,

LinearLayout.LayoutParams params = (LinearLayout.LayoutParams)_viewlower.getLayoutParams();
    params.setMargins(70, 0, 0, 0);
    _viewlower.setLayoutParams(params);

,那么它就可以正常工作了。

请帮帮我。

【问题讨论】:

    标签: android


    【解决方案1】:

    在您的视图未显示之前,您不能使用 getLeft()(和 getTop 等)。无需手动计算边距,而是将边框放入 RelativeLayout 并使用 android:layout_below/android:layout_above/android:layout_toRightOf. 属性。

    【讨论】:

      猜你喜欢
      • 2014-07-02
      • 2012-06-29
      • 1970-01-01
      • 2013-04-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多