2015-07-02 6 views
-2

У меня есть текстовый вид, как это,TextView Совместите центр - Android

<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:textAppearance="?android:attr/textAppearanceLarge" 
    android:text="Large Text" 
    android:id="@+id/textView" 
    android:layout_centerHorizontal="true"/> 

Он выровнен в центре по горизонтали, но теперь я хочу, чтобы переместить его 20P влево от центра. Поэтому я пробовал android:layout_marginStart="30dp" android:layout_marginLeft="30dp"

Но он остался посередине, как я могу переместить его влево, но иметь ссылку на центр?

Здесь полно XML

<RelativeLayout 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:paddingLeft="@dimen/activity_horizontal_margin" 
android:paddingRight="@dimen/activity_horizontal_margin" 
android:paddingTop="@dimen/activity_vertical_margin" 
android:paddingBottom="@dimen/activity_vertical_margin" 
tools:context="com.spencer.one.SplashScreenActivity"> 

<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:textAppearance="?android:attr/textAppearanceLarge" 
    android:text="Large Text" 
    android:id="@+id/textView" 
    android:layout_centerHorizontal="true" 
    android:layout_marginStart="30dp" 
    android:layout_marginLeft="30dp"/> 
</RelativeLayout> 

Благодаря

+0

Пожалуйста, покажите свой полный код XML , –

+0

@ShoebSiddique см. Выше –

+0

Пожалуйста, посмотрите мой ответ. –

ответ

1
<TextView 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:textAppearance="?android:attr/textAppearanceLarge" 
     android:text="Large Text" 
     android:id="@+id/textView" 
     android:layout_marginLeft="30dp" 
     android:gravity="center"/> 

Содержание центр внутри TextView и 30dp MarginLeft

enter image description here

0

Вы можете попробовать android:paddingRight="30dp", что-то вроде этого.

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:gravity="center_horizontal" > 

    <TextView 
     android:id="@+id/textView" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginRight="30dp" 
     android:gravity="center" 
     android:text="Large Text" 
     android:textAppearance="?android:attr/textAppearanceLarge" /> 

</RelativeLayout> 
+0

нормально, что переместил его, но теперь оболочка содержимого не работает, вид распространяется дальше, чем символы? Есть ли другой способ? –

+0

См. Мои правки. –

+0

Это не перемещает текстовое изображение, его только в верхнем правом углу, он не двигается, я хочу, чтобы ссылка была центром всего представления, так что на любом экране текстовое изображение будет 30dp слева от центра этого экрана –

0

вы можете попробовать с этим кодом

<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:textAppearance="?android:attr/textAppearanceLarge" 
    android:text="Large Text" 
    android:id="@+id/textView" 
    android:gravity="left" 
    /> 
+0

Я хочу, чтобы ссылка была центром всего представления, так что на любом экране текст будет 30dp слева от центра этого экрана. –

0

Попробуйте это:

<TextView 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:textAppearance="?android:attr/textAppearanceLarge" 
android:text="Large Text" 
android:id="@+id/textView" 
android:layout_gravity="center_horizontal" 
android:layout_marginRight="20dp"/> 

Вы пытаетесь переместить его влево, таким образом делая запас 20dp в праве будет нажмите его на 20dp влево и сохраните центр в качестве эталона.

+0

Это вообще не перемещает текстовое представление, я хочу, чтобы ссылка была центром от всего представления, так что на любом экране текст будет 30dp слева от центра этого экрана. –

+0

О, я пробовал делать это без всякой информации о макете, и это сработало, но теперь все по-другому. Попробуйте вместо этого заполнить, измените последние две строки решения, которое я отправил: android: layout_centerHorizontal = "true" android: paddingRight = "30dp" –

0

немного сложный, но работает.

<LinearLayout 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_centerHorizontal="true" 
    > 
    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="whatever" 
     /> 
    <View 
     android:layout_width="20dp" 
     android:layout_height="match_parent" 
     /> 
</LinearLayout> 

Сначала создайте linearlayout и разместите текст внутри. И добавьте пустой вид, чтобы дать отступы.

все.

+0

Также вам нужно добавить тег ориентации в lineralayout. –

0

вы должны использовать отступы вместо края для ...

<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:textAppearance="?android:attr/textAppearanceLarge" 
    android:text="Large Text" 
    android:id="@+id/textView" 
    android:layout_centerHorizontal="true" 
    android:paddingRight="30dp" 
/> 
Смежные вопросы