【问题标题】:setOnClickListener in RelativeLayout not working相对布局中的 setOnClickListener 不起作用
【发布时间】:2018-02-15 09:47:36
【问题描述】:

我的 android 应用程序中有一个结构

LinearLayout > FrameLayout >ImageView(用作FrameLayout 背景) & RelativeLayout > TextView & ImageView。

当我点击 FrameLayoutRelativeLayout 或 FirstImageView (因为它是更大的区域,textView 或 2nd Image 视图很小)时,我需要它做一个动作。

我尝试将 setOnClickListener 添加到此项目,但没有成功。

这是我的代码:

public class HomeFragment extends Fragment {
    View rootView;

    public HomeFragment() {

    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        rootView = inflater.inflate(R.layout.home_fragment, container, false);

        RelativeLayout paleoFoodBackGround = (RelativeLayout) rootView.findViewById(R.id.relativeLytPaleoFoodHome);


        paleoFoodBackGround.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Log.d("1FOOD_LIST", "onClick: ");

            }
        });


        return rootView;
    }

    public void clickFood(){
        Log.d("2FOOD_LIST", "onClick: ");
    }


}

这是我的布局结构:

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="50"
android:orientation="vertical"
android:paddingLeft="2.5dp"
android:paddingRight="5dp"
android:weightSum="100">

    <FrameLayout
    android:id="@+id/framePaleoFoodHome"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_weight="50"
    android:paddingBottom="2.5dp">

        <ImageView
        android:id="@+id/imgViewPaleoFoodBackGround"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:scaleType="center"
        app:srcCompat="@drawable/recipes" />

        <RelativeLayout
        android:id="@+id/relativeLytPaleoFoodHome"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"

        >

            <ImageView
            android:id="@+id/imgViewRecipesIconHome"
            style="@style/iconHome"
            app:srcCompat="@drawable/cooking"
            />

            <TextView
            android:id="@+id/txtViewRecipesTxtHome"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            style="@style/textViewHomeSquare"
            android:text="@string/RecipesHome"

            android:layout_below="@id/imgViewRecipesIconHome"/>

        </RelativeLayout>

    </FrameLayout>
    <Framelayout> .... </Framelayout>
</LinearLayout>

【问题讨论】:

  • rootView 是什么?
  • 可能是您从错误的角度获取 id,分享适配器的所有内容。
  • 删除框架布局然后尝试
  • 请在此问题中添加您的返回视图
  • 添加返回视图并从相关布局中删除此android:onClick="clickFood"

标签: android


【解决方案1】:

我建议使用 ImageButton 而不是图像。还将您的 RelativeLayout 设置为具有 android:clickable="true"。同时设置所有不可点击的子视图 android:clickable="false"

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-01-20
    • 1970-01-01
    • 1970-01-01
    • 2018-05-12
    • 2014-11-08
    • 2012-07-13
    • 1970-01-01
    相关资源
    最近更新 更多