【问题标题】:How to seperate the finger touch feature in one layout and one activity?如何在一个布局和一个活动中分离手指触摸功能?
【发布时间】:2011-09-24 21:24:46
【问题描述】:

如果我的主要布局由两个线性布局组成,如下所示:

res/layout/main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

  <LinearLayout
      android:orientation="horizontal"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent"
      android:layout_weight="1">
      <TextView
          android:text="red"
          android:gravity="center_horizontal"
          android:background="#aa0000"
          android:layout_width="wrap_content"
          android:layout_height="fill_parent"
          android:layout_weight="1"/>

      <TextView
          android:text="blue"
          android:gravity="center_horizontal"
          android:background="#0000aa"
          android:layout_width="wrap_content"
          android:layout_height="fill_parent"
          android:layout_weight="1"/>
      <TextView
          android:text="yellow"
          android:gravity="center_horizontal"
          android:background="#aaaa00"
          android:layout_width="wrap_content"
          android:layout_height="fill_parent"
          android:layout_weight="1"/>
  </LinearLayout>

  <LinearLayout
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_weight="1">
    <TextView
        android:text="row one"
        android:textSize="15pt"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"/>
    <TextView
        android:text="row two"
        android:textSize="15pt"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"/>
    <TextView
        android:text="row three"
        android:textSize="15pt"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"/>
  </LinearLayout>

</LinearLayout>

我的活动类如下:

public class MainActivity extends Activity{
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
}

我想实现上层线性布局(包含颜色)可以通过手指触摸移动内容的功能,我必须实现OnTouchListener,但是对于下层线性布局(包含几个TextView)我做不想有手指触摸功能。

由于它们在一个 xml 布局文件中,并且我有一个 Java 类(MainActivity.java),如何将它们与一半的布局分开,具有手指触摸功能,但在整个主要活动类中实现 onTouchListener ?

【问题讨论】:

    标签: android android-layout android-emulator android-widget


    【解决方案1】:

    是的,有可能。您必须为该特定视图设置 ontouchlistener 。所以只有那个视图响应触摸事件。

    【讨论】:

    • 这就是我要问的问题,如何为该特定视图设置 ontouchlistener,因为它们都在一个 xml 文件中定义并在活动 java 类中引用。
    • 请将 android:id 提供给 linearlayout 或您想要触摸监听器的任何其他视图,然后在该视图上设置触摸监听器
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-07-28
    • 1970-01-01
    • 2014-09-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-15
    相关资源
    最近更新 更多