2010-12-07 2 views
2

나는 이전에이 작업을 수행했음을 확신하지만 어떻게 잊어 버렸습니다.TextView를 단추의 오른쪽으로 유지하면서 가운데에 맞 춥니 다?

여기에 문제가 있습니다. 버튼과 텍스트 뷰가 있는데 버튼이 왼쪽에있는 동안 텍스트 뷰를 중앙에 배치하고 싶습니다.

문제 없습니까? 그냥 relativelayout에 넣고 textview centerinparent, button alignparentleft를 만듭니다.

하지만 이제 동적으로 텍스트를 변경하므로 버튼 상단에 잠재적으로 작성할 수 있습니다. 텍스트 뷰에서 toRightOf = "@ id/button"을 추가합니다. 아니, 이제는 더 이상 중심에 있지 않습니다.

스크린 샷을 제공하고 싶지만 컴퓨터가 메모리가 부족하여 그렇게 할 수없는 것 같습니다.

는 여기에 몇 가지 코드입니다 : http://pastebin.com/3N70Vjre

<RelativeLayout 
     android:id="@+id/header" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignParentTop="true"> 
     <Button 
       android:id="@+id/leftbutton" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_alignParentLeft="true" 
       android:layout_centerVertical="true" 
       android:text="text!" 
       /> 
     <TextView 
       android:id="@+id/toptext" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:textColor="@android:color/white" 
       android:layout_toRightOf="@id/leftbutton" 
       android:layout_centerInParent="true" 
       android:textSize="16sp" 
       android:textStyle="bold" 
       android:text="Text!" 
       android:singleLine="true" 
       /> 
    </RelativeLayout> 
+0

당신이 명확하게 할 수에서 당신을 지적하는 데 도움이?어느 것이 작동하고 어느 것이 작동하지 않습니까? – mibollma

+0

저는 수평 중심에 대해 이야기하고 있습니다. relativelayout 내의 수직 센터링은 정상적으로 작동합니다. – AmITheRWord

답변

3

이 시도 (불행하게도 나는 이클립스에 뛰어들 수 있도록 작업에있어 몇 가지 코드를 얻을 수 있습니다) -

  • 변경의 layout_width를 fill_parent에 TextView.
  • TextView의 중력을 가운데로 설정하십시오 (TextView 안에 텍스트 센터가 있음)
  • Button의 layout_weight를 1로 설정하고 TextView의 layout_weight를 2로 설정하십시오.이 숫자로 퍼지해야 할 수도 있습니다 당신이 찾고있는 레이아웃을 얻으려면.

TextView 자체를 가운데 정렬하지는 않지만이 단추 다음에 TextView 텍스트를 가운데 맞추어야합니다. TextView를 컨테이너 (Linear/Relative Layout)로 대체하고 TextView 대신 Layout에서 위와 동일한 방법을 사용하면됩니다. 그러면 TextView를 컨테이너 안에 넣고 컨테이너의 중력을 "가운데"로 설정합니다.

희망이 올바른 방향 : 당신은 수평 또는 수직 중심에 대해서 이야기하는 경우

+0

감사합니다. 나는 그걸 반감기로 삼았고, 당신은 나의 해결책에 가장 가까웠습니다. 그래서 나는 대답을 당신에게 줄 것입니다. – AmITheRWord

0

다음 텍스트 뷰의 왼쪽에있는 버튼을 정렬, 먼저 텍스트 뷰를 선언하십시오 (내가 XML을 붙여 넣을 수 없기 때문에 ...?). TextView가 너무 넓어지면 문제가 발생할 수 있습니다.

EDIT : 내가보기에, 뒤로/다음 버튼 (어쨌든 비슷한)으로 iPhone의 헤더와 같은 것을하려고합니다. 이 수정을 시도하십시오. 그래도 TextView가 Button을 치기에 충분히 커지면 문제가 발생할 것이라고 저는 믿습니다.

<RelativeLayout 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     android:id="@+id/header" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignParentTop="true" 
     > 
     <TextView 
      android:id="@+id/toptext" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:textColor="@android:color/white" 
      android:layout_alignParentCenter="true" 
      android:textSize="16sp" 
      android:textStyle="bold" 
      android:text="Text!" 
      android:singleLine="true" 
      /> 
     <Button 
      android:id="@+id/leftbutton" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignParentLeft="true" 
      android:text="text!" 
      />   
    </RelativeLayout> 

대신이 FrameLayout이보십시오. 이것은 당신이 기대하는지 더 많은 일을 할 수

<FrameLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    > 
    <TextView 
     android:id="@+id/toptext" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:gravity="center" 
     android:textColor="@android:color/white" 
     android:textSize="16sp" 
     android:textStyle="bold" 
     android:text="Text!" 
     android:singleLine="true" 
     /> 
    <Button 
     android:id="@+id/leftbutton" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Text!" 
     /> 
</FrameLayout> 
+0

그러면 텍스트보기 바로 옆에 단추가 놓이고 둘 다 가운데에 배치됩니다. 이제 버튼의 alignParentLeft = "true"로 설정하면 버튼이 textview에 도달 할 때까지 버튼이 늘어납니다. 그리고 텍스트보기가 너무 넓어지면 단추가 밀려납니다. 내 문제를 근본적으로 뒤집어 놓았습니다. P – AmITheRWord

+0

정확히 왜이 일을 하려는지 설명해 주시겠습니까? 이 인스턴스가 작동하는지 여부에 관계없이이 유형의 레이아웃으로 문제가 발생하는 것처럼 보입니다. – kcoppock

+0

내 레이아웃의 오른쪽 버튼을 제외한 http://img801.imageshack.us/img801/4372/tablelayout.png이 태그 바로 옆에 Textview가 매시 져 있고 패딩이나 여백을 사용하여 푸시하는 것보다 낫습니다. . – AmITheRWord

0

이 (의사 코드)를 시도 할 수 있습니다 :

<RelativeLayout> 
    <Button> 
    <LinearLayout toLeftOf="toptext" type="horizontal"> 
    <TextView gravity="center"> 
    </LinearLayout> 
</RelativeLayout> 

당신은있는 LinearLayout 폭 = "fill_parent"등을해야 할 수도 있습니다. 그게 효과가 있을지 확실하지 않습니다. 당신은 나중에 여기에 나열된 몇 가지를 시도 할 수 있습니다 : http://thinkandroid.wordpress.com/2010/01/14/how-to-position-views-properly-in-layouts/

+0

그 중 하나가 작동하지 않습니다. 그리고 만약 그렇다면, 텍스트 뷰는 Relativelayout 내부가 아닌 Linearlayout 내부에서 수평 중심으로 배치됩니다. 중앙 + 버튼의 너비입니다. 어쩌면 단추의 너비를 가져 와서 텍스트 뷰의 왼쪽 여백에서 뺄 수있는 방법이 있을까요? : \ – AmITheRWord

+0

너비가 fill_parent 인 경우가 아닙니다. LinearLayout은 나머지 가로 공간을 채우고 그 안에 TextView를 가운데에 배치합니다. – methodin

+0

예, 버튼을 포함하지 않은 부분 만 채울 수 있습니다. – AmITheRWord

관련 문제