0

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>

私の onClickListener と AndroidAnnotations

 @ViewById
 ImageButton btn_newsdet_back;


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

TextViewの下にある場合、 EDIT onClickは機能しています(呼び出されています)。誰でも理由を説明できますか?

ここに画像の説明を入力

4

5 に答える 5

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に配置され、Fragment正常に動作しています。

于 2014-11-06T11:31:20.617 に答える
0

あなたが言ったように、ボタンを RelativeLayout 内に配置するとすべてが機能する場合、その理由は、2 番目の RelativeLayout のパラメーターが次のように宣言されているためです。

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

これは、ボタンを含む元の RelativeLayout である親を「埋める」ことを意味します。ボタンを覆い、ボタンとのやり取りが機能しないようにするレイヤーを作成しているようなものです。ボタンをクリックすると、実際には 2 番目の RelativeLayout をクリックしています。

私は実際にこれを試していませんが、次の設定を検討する必要があります。

android:layout_below="@id/btn_newsdet_back"

ボタンの下の RelativeLayout。

于 2014-11-06T09:52:26.610 に答える
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

私はそれを解決しました。問題は、探していたオーバーレイである透明なアクションバーを使用していたことです。

私の onCreate() の次の行はこれを解決しました:

getActivity().getActionBar().hide();
于 2014-11-06T12:59:36.310 に答える
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 に答える