我试图将Text放在TableLayout的TextView中心,但不知怎的,它在安装的应用程序中不起作用。它可能是由我设置为0dp的宽度引起的。我这样做是为了让表格权重正常工作。
这是活动的XML布局:
<TextView
android:id="@+id/text_sign_in"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Log in with..."
android:layout_below="@id/toolbar_account"
android:layout_marginTop="10dp"
android:layout_marginStart="10dp"
android:textSize="20sp"/>
<TableLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/text_sign_in"
android:layout_marginTop="10dp">
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="10dp">
<de.hdodenhof.circleimageview.CircleImageView
android:id="@+id/image_logo_instagram"
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_weight="0.2"
android:layout_marginStart="@dimen/acc_image_margin_start"
android:contentDescription="Profile Image"
android:elevation="@dimen/acc_image_elevation"
android:outlineProvider="bounds"
android:src="@drawable/ic_instagram" />
<TextView
android:id="@+id/text_instagram_label"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.4"
android:layout_marginStart="10dp"
android:text="@string/label_instagram"
android:textSize="20sp"
android:gravity="center_vertical"/>
<Button
android:id="@+id/button_instagram_login"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.4"
android:drawableLeft="@color/color_youtube"
android:text="Log In"
android:background="@android:color/transparent"
android:gravity="center"/>
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="10dp">
<de.hdodenhof.circleimageview.CircleImageView
android:id="@+id/image_logo_twitter"
android:contentDescription="Profile Image"
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_weight="0.2"
android:src="@drawable/ic_twitter"
android:elevation="@dimen/acc_image_elevation"
android:outlineProvider="bounds"
android:layout_marginStart="@dimen/acc_image_margin_start"/>
<TextView
android:id="@+id/text_twitter_label"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.4"
android:layout_marginStart="10dp"
android:text="@string/label_twitter"
android:textSize="20sp"
android:gravity="center_vertical|start"/>
<com.maximutan.socialmedia_feed_merger.widgets.CustomTwitterLoginButton
android:id="@+id/button_twitter_login"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.4"
android:text="Log In"
android:gravity="center"/>
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="10dp">
<de.hdodenhof.circleimageview.CircleImageView
android:id="@+id/image_logo_twitch"
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_weight="0.2"
android:layout_marginStart="@dimen/acc_image_margin_start"
android:contentDescription="Profile Image"
android:elevation="@dimen/acc_image_elevation"
android:outlineProvider="bounds"
android:src="@drawable/ic_twitch" />
<TextView
android:id="@+id/text_twitch_label"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.4"
android:text="@string/label_twitch"
android:textSize="20sp"
android:layout_marginStart="10dp"
android:gravity="center_vertical" />
<Button
android:id="@+id/button_twitch_login"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.4"
android:drawableLeft="@color/color_youtube"
android:text="Log In"
android:background="@android:color/transparent"
android:gravity="center"/>
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="10dp">
<de.hdodenhof.circleimageview.CircleImageView
android:id="@+id/image_logo_facebook"
android:contentDescription="Profile Image"
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_weight="0.2"
android:src="@drawable/ic_facebook"
android:elevation="@dimen/acc_image_elevation"
android:outlineProvider="bounds"
android:layout_marginStart="@dimen/acc_image_margin_start"/>
<TextView
android:id="@+id/text_facebook_label"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.4"
android:text="@string/label_facebook"
android:textSize="20sp"
android:layout_marginStart="10dp"
android:gravity="center_vertical" />
<Button
android:id="@+id/button_facebook_login"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.4"
android:drawableLeft="@color/color_youtube"
android:text="Log In"
android:background="@android:color/transparent"
android:gravity="center"/>
</TableRow>
</TableLayout>
这是“活动”在Android Studio编辑器中的显示内容:
这是执行后的活动:
修改
我认识到有时在重新打开正在运行的应用程序之后问题消失并且文本正确显示就像在此图像中一样,这让我想知道它是否与代码有关,而不是在我取消暂停活动后执行
//CHECK TOKEN VALIDITY AFTER THE LOGIN IS FINISHED
@Override
protected void onResume() {
super.onResume();
refreshLayout();
}
//REFRESHES LAYOUT AFTER INTERNAL CHANGES
private void refreshLayout(){
if(NetworkManager.getInstance().isValid(ConstantsHelper.ID_INSTAGRAM)){
mInstagramButton.setText(getString(R.string.button_signed_in));
mInstagramButton.setTextColor(getResources().getColor(R.color.color_text_secondary_light));
mInstagramButton.setCompoundDrawablesWithIntrinsicBounds(R.drawable.ic_done, 0, 0, 0);
}
if(NetworkManager.getInstance().isValid(ConstantsHelper.ID_TWITCH)){
mTwitchButton.setText(getString(R.string.button_signed_in));
mTwitchButton.setTextColor(getResources().getColor(R.color.color_text_secondary_light));
mTwitchButton.setCompoundDrawablesWithIntrinsicBounds(R.drawable.ic_done, 0, 0, 0);
}
if(NetworkManager.getInstance().isValid(ConstantsHelper.ID_FACEBOOK)){
mFacebookButton.setText(getString(R.string.button_signed_in));
mFacebookButton.setTextColor(getResources().getColor(R.color.color_text_secondary_light));
mFacebookButton.setCompoundDrawablesWithIntrinsicBounds(R.drawable.ic_done, 0, 0, 0);
}
if(NetworkManager.getInstance().isValid(ConstantsHelper.ID_TWITTER)){
mTwitterButton.setText(getString(R.string.button_signed_in));
mTwitterButton.setTextColor(getResources().getColor(R.color.color_text_secondary_light));
mTwitterButton.setCompoundDrawablesWithIntrinsicBounds(R.drawable.ic_done, 0, 0, 0);
}
}
答案 0 :(得分:1)
问题的可能解决方案是将TexTview
与LinearLayout
包围,并为其TextView
提供宽度和高度,如:
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.4"
然后,您可以match_parent
使用TextView
作为gravity
的宽度和高度。这将使您可以使用gravity
进行操作。因此,您可以将TextView
centre
设置为{{1}}。
答案 1 :(得分:0)
如何将重力设置为中心,例如。 android:gravity="center"
代替android:gravity="center_vertical"