我需要GridLayout像这样对齐视图(浅蓝色图标和文本是GridLayout中的视图):
如果我设置app:layout_columnWeight="1"
and app:layout_rowWeight="1"
,我的GridLayout会像这样对齐:
我的GridLayout xml:
<android.support.v7.widget.GridLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="18dp"
app:columnCount="7">
<com.motivapp.motivapp.customview.KeywordView
android:id="@+id/keyword1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
keyword:text="Trilha"/>
<com.motivapp.motivapp.customview.KeywordView
android:id="@+id/keyword2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
keyword:text="Mergulho"/>
<com.motivapp.motivapp.customview.KeywordView
android:id="@+id/keyword3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
keyword:text="Praia"/>
<com.motivapp.motivapp.customview.KeywordView
android:id="@+id/keyword4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
keyword:text="Festas"/>
<com.motivapp.motivapp.customview.KeywordView
android:id="@+id/keyword5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
keyword:text="Parque de Diversões"/>
<com.motivapp.motivapp.customview.KeywordView
android:id="@+id/keyword6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
keyword:text="Relaxar"/>
<com.motivapp.motivapp.customview.KeywordView
android:id="@+id/keyword7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
keyword:text="Esportes Radicais"/>
<com.motivapp.motivapp.customview.KeywordView
android:id="@+id/keyword8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
keyword:text="Acampamento"/>
<com.motivapp.motivapp.customview.KeywordView
android:id="@+id/keyword9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
keyword:text="Off Road"/>
<com.motivapp.motivapp.customview.KeywordView
android:id="@+id/keyword10"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
keyword:text="Passeio de Barco"/>
<com.motivapp.motivapp.customview.KeywordView
android:id="@+id/keyword11"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
keyword:text="Restaurantes"/>
<com.motivapp.motivapp.customview.KeywordView
android:id="@+id/keyword12"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
keyword:text="Show / Festival"/>
<com.motivapp.motivapp.customview.KeywordView
android:id="@+id/keyword13"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
keyword:text="Resort / SPA"/>
<com.motivapp.motivapp.customview.KeywordView
android:id="@+id/keyword14"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
keyword:text="Locas Históricos"/>
</android.support.v7.widget.GridLayout>
GridView中的视图:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<com.motivapp.motivapp.customview.DefaultTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/keywordTextView"
android:textSize="18sp"
android:text="Keyword"
android:textColor="#839EC5"
android:layout_gravity="center_horizontal"/>
<ImageView
android:layout_width="130dp"
android:layout_height="130dp"
android:layout_gravity="center_horizontal"
android:src="@drawable/keyword_button"
android:id="@+id/keywordButton"/>
</LinearLayout>
这是一个自定义视图,但现在,它什么都不做,只显示自定义textview和imageview ...我对GridLayout做的事情与第一张图像对齐?
更新
接近解决我的问题......我改变了一些事情,现在只有一个视图没有对齐。
我将app:layout_columnWeight="1"
放在gridlayout的所有视图上,并更改了自定义视图布局的重力和文字宽度:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|center_horizontal">
<com.motivapp.motivapp.customview.DefaultTextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/keywordTextView"
android:textSize="16sp"
android:text="Keyword"
android:textColor="#839EC5"
android:gravity="center"/>
<ImageView
android:layout_width="120dp"
android:layout_height="120dp"
android:src="@drawable/keyword_button"
android:id="@+id/keywordButton"/>
</LinearLayout>
如何集中所有这些观点?如何防止当textview有一个很大的宽度和换行时,他强迫imageview下去而不是居中对齐?
答案 0 :(得分:1)
这是因为textview大小很长,这就是为什么它需要更多的空间所以请在你的match_parent中设置宽度 textview如果不起作用,则减少文本大小。
答案 1 :(得分:1)
您可以将自定义GridView与android:numColumns="7"
一起使用,也可以将RecyclerView与GridLayoutManager一起使用7列
您可以使用项目视图的自定义布局来定义文本和图像,如下所示:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/txtGridItem"
android:layout_width="match_parent"
android:gravity="center"
android:layout_height="wrap_content" />
<ImageView
android:id="@+id/imgGridItem"
android:layout_width="match_parent"
android:layout_height="130dp" />
</LinearLayout>
答案 2 :(得分:1)
这是一种方法。这假设您希望GridView以父对象为中心。如果它需要相对于其他东西居中,则必须修改代码。
我所做的只是onLayout的子类和&#34;翻译&#34;网格视图到中心位置。
public class GridViewCentering extends GridView
{
public GridViewCentering (Context context, AttributeSet attrs)
{
super (context, attrs);
}
@Override
protected void onLayout (boolean changed, int l, int t, int r, int b)
{
super.onLayout (changed, l, t, r, b);
int parentW = ((ViewGroup)getParent ()).getMeasuredWidth();
int w = getWidth ();
int offset = (parentW - w) / 2;
setTranslationX (offset);
}
}
答案 3 :(得分:0)
使用 RecyclerView 代替 GridLayout 并将计数设置为7.
答案 4 :(得分:0)
在xml中使用
<GridView
android:id="@+id/grid"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:columnWidth="100dp"
android:gravity="center"
android:numColumns="auto_fit"
android:stretchMode="columnWidth" />
答案 5 :(得分:0)
你可以像这样使用RecyclerView和GridLayoutManager
GridLayoutManager mGridLayoutManager = new GridLayoutManager(context, 2, LinearLayoutManager.HORIZONTAL, false);
将布局管理器设置为适配器
mRecyclerView.setLayoutManager(mGridLayoutManager);
答案 6 :(得分:0)
问题在于,当文本只有一两行时,视图的高度不相同。 有多种可能的解决方案:
最好是: 将 layout_gravity =“ bottom” 添加到Gridlayout内的每个项目。如果有一个包含两行的视图,则将其他视图的间距添加到顶部,并且图像(圆圈)的高度都相同。
如果您希望所有行都具有相同的高度,则可以将 android:minLines =“ 2” 添加到所有Textviews中。但这会在文本和图像之间留出一段短文本的距离。