0

任何想法为什么我的 ImageButton 在我的片段中不起作用?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>

我的 onClickListener 与 AndroidAnnotations

 @ViewById
 ImageButton btn_newsdet_back;


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

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

在此处输入图像描述

4

5 回答 5

0

@覆盖

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();

        }



    });     
}
于 2014-11-06T11:54:00.420 回答
0

你可以试试这段代码:

这是片段的代码: 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放置在 a 中Fragment,它工作正常。

于 2014-11-06T11:31:20.617 回答
0

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

<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
}
于 2014-11-06T11:50:29.003 回答
0

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

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

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

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

android:layout_below="@id/btn_newsdet_back"

在按钮下方的 RelativeLayout 上。

于 2014-11-06T09:52:26.610 回答
0

我解决了。问题是我使用的是透明的操作栏,这是我正在寻找的覆盖。

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

getActivity().getActionBar().hide();
于 2014-11-06T12:59:36.310 回答