【问题标题】:Android : GridLayout Highlight A RowAndroid:GridLayout突出显示一行
【发布时间】:2015-12-01 14:21:06
【问题描述】:


我正在为我的应用程序编写反馈屏幕并为其使用网格布局。
这是一个静态表单,显示图像和文本描述。
我想突出显示选定的项目,以便用户知道点击事件。
虽然 listSelector 设置背景可绘制对象不可用用于网格布局; refNowTxt.setBackgroundColor(getResources().getColor(R.color.frame_background)) 没有突出显示整行。
我正在考虑检索选定的行并设置背景以突出显示。
感谢任何帮助。
以下是我的layout.xml:

<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:useDefaultMargins="true"
    android:alignmentMode="alignBounds"
    android:columnOrderPreserved="false"
    android:columnCount="2"
    android:orientation="horizontal"
    android:rowCount="7"
    android:paddingTop="6dp">

    <ImageView
        android:id="@+id/imgWriteFeedback"
        android:layout_width="wrap_content"
        android:layout_gravity="left"
        android:layout_row="0"
        android:layout_column="0"
        android:src="@drawable/write_to_us"
        android:paddingTop="6dp"
        android:paddingLeft="6dp"
        android:paddingRight="6dp"
        android:paddingBottom="6dp"/>

    <TextView
        android:id="@+id/lblWriteFeedback"
        android:layout_row="0"
        android:layout_column="1"
        android:textSize="20sp"
        android:gravity="left"
        android:textColor="@android:color/black"
        android:layout_marginLeft="5dp"
        android:text="@string/write_to_us"
        android:paddingTop="6dp"
        android:layout_gravity="left"
        />

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="1px"
        android:layout_marginTop="3dp"
        android:background="@color/frame_background"
        android:orientation="horizontal"
        android:layout_columnSpan="2"/>

    <ImageView
        android:id="@+id/imgRateUs"
        android:layout_width="wrap_content"
        android:layout_gravity="left"
        android:layout_row="1"
        android:layout_column="0"
        android:src="@drawable/rate_us_resized"
        android:paddingTop="6dp"
        android:paddingLeft="6dp"
        android:paddingRight="6dp"
        android:paddingBottom="6dp"/>

    <TextView
        android:id="@+id/lblRateUs"
        android:layout_row="1"
        android:layout_column="1"
        android:textSize="20sp"
        android:gravity="left"
        android:textColor="@android:color/black"
        android:layout_marginLeft="5dp"
        android:paddingTop="6dp"
        android:text="@string/rate_us"
        />

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="1px"
        android:layout_marginTop="3dp"
        android:background="@color/frame_background"
        android:orientation="horizontal"
        android:layout_columnSpan="2"/>

    <ImageView
        android:id="@+id/imgShareApp"
        android:layout_width="wrap_content"
        android:layout_gravity="left"
        android:layout_row="2"
        android:layout_column="0"
        android:src="@drawable/share_resized"
        android:paddingTop="6dp"
        android:paddingLeft="6dp"
        android:paddingRight="6dp"
        android:paddingBottom="6dp"/>

    <TextView
        android:id="@+id/lblShareApp"
        android:layout_row="2"
        android:layout_column="1"
        android:textSize="20sp"
        android:gravity="left"
        android:textColor="@android:color/black"
        android:layout_marginLeft="5dp"
        android:paddingTop="6dp"
        android:text="@string/share_app"
        />

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="1px"
        android:layout_marginTop="3dp"
        android:background="@color/frame_background"
        android:orientation="horizontal"
        android:layout_columnSpan="2"/>

</GridLayout>

`

【问题讨论】:

标签: android highlight selected android-gridlayout


【解决方案1】:
Inside adapter create a method,
private selectedIndex;
public void selectedIndex(int gridSelectionPosition)
{
 this.selectedIndex=gridSelectionPosition;
  notifyDataSetChanged();
}

and then inside getView method

Do something like this, 

 getView()
{
if(position==selectedIndex)
{
   view.setBackgroundColor(color);

}
else
{
  view.setBackgroundColor(white);
}

inside your gridview.OnItemClickListener

adapter.selectedIndex(position);

【讨论】:

  • 谢谢。附上布局代码。在我的情况下不使用适配器。
  • Inside grid OnclickListener set View.setBackgroundColor(Color) 试试这个家伙。或 android:listselector="@color/yourcolor"。您也可以点击链接 Mustaneer 回答。您可以使用可用于 gridview 的 android:listSelector 来...请检查一下。
  • GridLayout coreLayout = (GridLayout) rootView.findViewById(R.id.settingsGrid); coreLayout.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Log.i("Settings Fragment","Grid View Item Clicked:"+view.getId()); } }); 在这种情况下不会被调用。
  • 您好,我的代码中没有 GridView 组件; GridLayout 没有 setOnItemClickListener 方法。布局 xml 具有 GridLayout 和以静态方式设置的视图。
  • gridLayout,对不起,伙计,没有想法。
猜你喜欢
  • 1970-01-01
  • 2011-10-01
  • 2017-01-17
  • 2013-03-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多