【问题标题】:how to set visibility for imagepager and linear layout如何设置 imagepager 和线性布局的可见性
【发布时间】:2016-10-20 20:39:54
【问题描述】:

我在布局中有这个寻呼机,我尝试根据条件为寻呼机和线性布局设置可见性,但它不起作用我在互联网上搜索并且都有相同的答案,知道吗?

这是xml

<LinearLayout
    android:orientation="vertical"
    android:id="@+id/error_linear_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:weightSum="1"
    android:visibility="visible">
    <ImageView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:srcCompat="@mipmap/ic_launcher"
        android:id="@+id/error_image_view"
        android:layout_weight="0.59" />
    <TextView
        android:text="This Screen Is Not Active"
        android:textSize="30sp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/error_text_view"
        android:layout_weight="0.59" />
</LinearLayout>

<android.support.v4.view.ViewPager
        android:id="@+id/pager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:visibility="visible"></android.support.v4.view.ViewPager>

这是主要活动

ViewPager imagePager;
LinearLayout errorLayout;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_image_pager);

    //views
    errorLayout = (LinearLayout) findViewById(R.id.error_linear_layout);
    imagePager = (ViewPager) findViewById(R.id.pager);
    IsActive();
}
public void IsActive()
{
    String x = "x";

    if (x=="x")
    {
        imagePager.setVisibility(View.VISIBLE);
        errorLayout.setVisibility(View.INVISIBLE);
    }else
    {
        imagePager.setVisibility(View.INVISIBLE);
        errorLayout.setVisibility(View.VISIBLE);
    }
}

【问题讨论】:

  • 用x.equals("x");替换x == "x"
  • 不工作可见性错误因为我在 onCreate 中尝试没有任何方法并且不工作
  • 在java中你永远不应该将字符串(当你对它们的内容感兴趣时)与'=='进行比较
  • 好的,我将其编辑为 x.equals("x");并且不工作错误在于可见性
  • 哪个错误,能贴一下日志吗?

标签: android android-viewpager


【解决方案1】:

也许错误不是视图不可见,而是:

x 永远不能等于“x”。变量 X 是一个引用,而不是它的实际表示。

用途:

改为if(x.equals("x"))。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-26
    • 1970-01-01
    相关资源
    最近更新 更多