【发布时间】:2018-02-15 09:47:36
【问题描述】:
我的 android 应用程序中有一个结构
LinearLayout > FrameLayout >ImageView(用作FrameLayout 背景) & RelativeLayout > TextView & ImageView。
当我点击 FrameLayout 或 RelativeLayout 或 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