android - 在父 View 上触摸或调用 setPressed() 会更改 TextView 的可见性

标签 android android-layout

包含 TextView 的自定义 View 会发生一些非常奇怪的事情。粗略地说,(相关代码如下),我有一个自定义 View (FrameLayout 的子类),它会膨胀一些 xml 并将生成的 View 添加为 subview 。膨胀的 View 包含一个 TextView,下面称为“标签”。另外可能相关的(如果您不打算查看代码)是自定义 View 作为 subview 添加到 MapView 中。

发生了以下非常奇怪的事情:

  • 什么都不做:标签不可见,父级看起来很好。有趣的是,父 View 显然考虑了标签的宽度(将标签可见性设置为 GONE 时,父 View 要窄得多)。在设计模式下,标签可见。
  • 触摸父 View :标签短暂可见,然后消失。将手指放在上面即可保持标签可见,直到您结束触摸。
  • 调用 this.setPressed(true)this 当然是父 View ,从父 View 的构造函数中:标签变得可见,并保持这种状态直到父 View View 被触摸,之后行为恢复为“正常”。

这是相关代码。作为上下文,当您触摸 map 时,我们会在 map 上粘贴一个气球,其中应该包含类似于“点击以选择此位置”的内容。气球中还有一些其他 View ,但它们的可见性最初设置为 GONE,并且填充和显示它们的代码被禁用。不涉及 map 叠加,只需使用 MODE_MAP 将 View 直接附加到 map ,以将其粘贴到单个位置。

来自自定义 View BalloonView.java。这个类中没有其他内容涉及布局/ View 方法。

public BalloonView(Context context) {
    super(context);

    if(!isInEditMode()){ // RoboGuice doesn't like edit mode
        RoboGuice.getInjector(context).injectMembersWithoutViews(this);
    }

    LayoutInflater inflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    // We're *not* attaching the view with it's default layout params.
    View v = inflater.inflate(R.layout.balloon_view, this, false); 
    FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(
            ViewGroup.LayoutParams.MATCH_PARENT,
            ViewGroup.LayoutParams.MATCH_PARENT);
    params.gravity = Gravity.NO_GRAVITY;

    addView(v,params);

    label = (TextView) v.findViewById(R.id.label);
    addressContainer = v.findViewById(R.id.addressContainer);
    address1 = (TextView)findViewById(R.id.address1);
    address2 = (TextView) findViewById(R.id.address2);

    // makes label visible initially
    // setPressed(true);

}

    // This is the constructor that we actually call, in case it matters...  
public BalloonView(Context context, OnClickListener onClick) {
    this(context);
    setOnClickListener(onClick);
}

balloon_view.xml

    <TextView
        android:id="@+id/label"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="6dip"
        android:layout_marginLeft="6dip"
        android:layout_marginRight="6dip"
        android:layout_marginTop="6dip" 
        android:text="@string/map_balloon_label"
        android:enabled="true"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <LinearLayout
        android:id="@+id/addressContainer"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="6dip"
        android:layout_marginLeft="8dip"
        android:visibility="gone" >

        <ImageView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:contentDescription="@string/place" 
            android:src="@drawable/location_place" />

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dip"
            android:orientation="vertical" >

            <TextView
                android:id="@+id/address1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"  
                android:textAppearance="?android:attr/textAppearanceSmall"
                android:visibility="visible" />

            <TextView
                android:id="@+id/address2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" 
                android:textAppearance="?android:attr/textAppearanceSmall" />
        </LinearLayout>

    </LinearLayout>

</LinearLayout>

这里是我们将 BalloonView 附加到 MapView 的地方 私有(private)MapView.LayoutParams getBalloonViewLayoutParams(GeoPoint where){ 返回新的 MapView.LayoutParams( ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, 其中,MapView.LayoutParams.BOTTOM_CENTER); }

private void showBalloon(QPGeoPoint where) {
    latitude = where.getLatitude();
    longitude = where.getLongitude();
    BalloonView bv = getBalloonView();
    if(bv.getParent() != mapView){
        Ln.d("bv.getParent()!=mapView");
        mapView.addView(balloonView, getBalloonViewLayoutParams(where));
    }else{
        Ln.d("parent was map view");
        balloonView.setLayoutParams(getBalloonViewLayoutParams(where));
    }
    balloonView.setVisibility(View.VISIBLE);
    balloonView.setLocation(where); 
}

其他(可能)相关的事情:

  • 我们正在使用 ActionBarSherlock、RoboGuice 和 roboguice-sherlock 插件。

  • 应用程序主题设置为 Sherlock.Theme.Light.DarkActionBar,但相关 Activity 的主题设置为 Theme.DeviceDefault.NoTitleBar

我完全困惑了,几个小时以来我一直在试图解决这个问题,并将继续这样做,并在发现新线索时发布更新。

最佳答案

女士们先生们,这里是:

文本和背景都是白色的。触摸文本将其颜色状态设置为按下,这使其黑色

快速解决方法是更改​​ TextView 上的 textColor 属性。

更一般的修复可能是为此 Activity 使用不同的主题,例如 Theme.DeviceDefault.Light.NoTitleBar

感觉这个很蠢;P

关于android - 在父 View 上触摸或调用 setPressed() 会更改 TextView 的可见性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12906301/

相关文章:

android - 如何垂直居中图表(使用 mpandroidchart 中的 LineChart)?

android - 如何在App上显示一次启动画面?

Android 在 XML 中设置 Bottom Sheet 单状态

android - 根据用户意愿更改背景图像

java - ImageView 和 Intent

c# - Xamarin 分步向导 android View

android - OkHttp 与 socket.io 兼容吗?

java - Youtube HTML5 视频在 Android 中停止运行

android - fitSystemWindows 以编程方式实现状态栏透明度

android - ClassNotFoundException:在 Android 9 上找不到类 SchemeRegistry