【问题标题】:ImageButton onClick not called in RelativeLayout in a Fragment using a transparent ActionBar使用透明 ActionBar 在 Fragment 中的 RelativeLayout 中未调用 ImageButton onClick
【发布时间】:2014-11-06 09:01:54
【问题描述】:

任何想法为什么我的 ImageButton 不能在我的 Fragment 中工作? ImageButton 包含一个 Drawable 并且是透明的。 当我将它放在下面的 RelativeLayout 中时,它的工作...

<RelativeLayout
android:id="@+id/rl_newsdet_root"
xmlns:android="http://schemas.android.com/apk/res/android"
android:background="@drawable/bg"
android:focusableInTouchMode="true"
android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView
    android:id="@+id/tv_newsdet_title"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textColor="@color/white"
    android:layout_marginTop="7dp"
    android:textSize="20sp"
    android:layout_centerHorizontal="true"
    />

<ImageButton
    android:id="@+id/btn_newsdet_back"
    android:layout_marginLeft="10dp"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:clickable="true"
    android:src="@drawable/bt_back"
    />

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginTop="?android:attr/actionBarSize"
    android:background="@color/white"
    android:gravity="left"
    >
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/tv_newsdet_name"
        android:textSize="18sp"
        android:layout_marginLeft="10dp"
        android:layout_marginTop="5dp"
        android:textStyle="bold"
        android:textColor="@color/cyan"
        android:background="@color/grey"
        android:paddingLeft="1dp"
        android:paddingRight="1dp" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/tv_newsdet_time"
        android:textColor="@color/white"
        android:textSize="18sp"
        android:layout_toRightOf="@+id/tv_newsdet_name"
        android:paddingLeft="1dp"
        android:paddingRight="1dp"
        android:background="@color/grey"
        android:layout_marginTop="5dp" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="New Text"
    android:id="@+id/tv_newsdet_text"
    android:layout_marginLeft="10dp"
    android:layout_marginTop="1dp"
    android:layout_below="@+id/tv_newsdet_name"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true" />
</RelativeLayout>

我的带有 AndroidAnnotations 的 onClickListener

 @ViewById
 ImageButton btn_newsdet_back;


    @Click(R.id.btn_newsdet_back)
    void back() {
       Log.i(TAG,"back");
       mActivity.popFragments();
    }

编辑 当 onClick 在 TextView 下方时,它正在工作(调用)。谁能解释一下为什么?

【问题讨论】:

  • “不工作”是什么意思?
  • 正如我在标题中所写,onClick 不起作用。对我来说,按钮似乎无法访问,但我不知道为什么。
  • 再次“不工作”并不是很清楚。日志是写的,还是不写的?只是没有执行的“popFragments()”吗?我们无法知道您期望的行为以及实际发生的情况。
  • onClick 不起作用:未调用 onClick。透明的 ActionBar 可能是一个可能的原因吗?
  • 我问了两个问题,你一个都没回答。

标签: android android-imagebutton


【解决方案1】:

如果如您所说,当您将按钮放在下面的 RelativeLayout 中时一切正常,原因应该是第二个 RelativeLayout 的参数声明为:

android:layout_width="match_parent"
android:layout_height="match_parent"

这意味着它正在“填充”其父级,即包含按钮的原始 RelativeLayout。这就像您正在创建一个覆盖按钮并阻止与它的任何交互工作的层。当您单击该按钮时,您实际上是在单击第二个 RelativeLayout。

我实际上并没有尝试过,但你应该考虑设置:

android:layout_below="@id/btn_newsdet_back"

在按钮下方的RelativeLayout上。

【讨论】:

    【解决方案2】:

    你可以试试这个代码:

    这是 Fragment 的代码: TestFragment.java

    public class TestFragment extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater,
            @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        // TODO Auto-generated method stub
    
        View view = inflater.inflate(R.layout.fragment_one, container, false);
    
        ImageButton button = (ImageButton) view.findViewById(R.id.img2);
    
        button.setOnClickListener(new OnClickListener() {
    
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Toast.makeText(getActivity(), "Click on fragment ",
                        Toast.LENGTH_LONG).show();
            }
        });
        return view;
    }
    
    }
    

    这是 'TestFragment`的布局

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
    
    <ImageButton
        android:id="@+id/img2"
        android:layout_width="100dp"
        android:layout_height="80dp"
        android:src="@drawable/ic_launcher" />
    
    </RelativeLayout>
    

    这是MainActivity.java的代码

    public class MainActivity extends FragmentActivity {
    
    ImageButton imgButton;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_main);
    
        FragmentManager fragmentManager = getSupportFragmentManager();
    
        TestFragment fragment = (TestFragment) fragmentManager
                .findFragmentById(R.id.fragment);
    
        FragmentTransaction fragmentTransaction = fragmentManager
                .beginTransaction();
        // fragmentTransaction.add(fragment, "");
        fragmentTransaction.commit();
    
    }
    
    }
    

    这是MainActivity的布局

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
    
    <fragment
        android:id="@+id/fragment"
        android:name="com.alarmdemo.TestFragment"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    
    </RelativeLayout>
    

    这里ImageButton 被放置在Fragment 中,它工作正常。

    【讨论】:

    • 感谢您的回答。正如您在我的编辑中看到的那样,onClick 在 TextView 下方时有效。所以一般来说它在一个片段中工作。似乎有一个我不明白的叠加层。
    • 你有两个 RelativeLayout 并且在宽度和高度上都有匹配的父级。尝试改变innerRelativeLayout的宽高
    • 设置宽度和高度以在内部布局中包裹内容无效。
    【解决方案3】:

    无需在图片按钮中添加可点击。尝试以下操作:

    <ImageButton
        android:id="@+id/btn_newsdet_back"
        android:layout_marginLeft="10dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="mymethod"
        android:src="@drawable/bt_back"
        />
    

    在java中添加

    public void mymethod(View v)
    {
      //your method
    }
    

    【讨论】:

    • 也许不需要设置可点击,但没有它也行不通。
    【解决方案4】:

    @Override

    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
    
        View rootView = inflater.inflate(R.layout.fragment_sample,
                container, false);
        addListenerOnButton();
    

    }

       private void addListenerOnButton() {
    
        imageButton = (ImageButton) rootview.findViewById(R.id.imageButton1);
    
        imageButton.setOnClickListener(new OnClickListener() {
    
            public void onClick(View arg0) {
    
               Toast.makeText(MainActivity.this,
                "ImageButton is clicked!", Toast.LENGTH_SHORT).show();
    
            }
    
    
    
        });     
    }
    

    【讨论】:

      【解决方案5】:

      我解决了。 问题是我使用了一个透明的操作栏,这是我正在寻找的叠加层。

      我的 onCreate() 中的以下行解决了这个问题:

      getActivity().getActionBar().hide();
      

      【讨论】:

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